home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / io.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  127.1 KB  |  5,073 lines  |  [TEXT/ALFA]

  1. # Functionality covered: operation of all IO commands, and all procedures
  2. # defined in generic/tclIO.c.
  3. #
  4. # This file contains a collection of tests for one or more of the Tcl
  5. # built-in commands.  Sourcing this file into Tcl runs the tests and
  6. # generates output for errors.  No output means no errors were found.
  7. #
  8. # Copyright (c) 1991-1994 The Regents of the University of California.
  9. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  10. #
  11. # See the file "license.terms" for information on usage and redistribution
  12. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13. #
  14. # SCCS: @(#) io.test 1.128 97/08/13 10:24:56
  15.  
  16. if {[string compare test [info procs test]] == 1} then {source defs}
  17.  
  18. if {"[info commands testchannel]" != "testchannel"} {
  19.     puts "Skipping io tests. This application does not seem to have the"
  20.     puts "testchannel command that is needed to run these tests."
  21.     return
  22. }
  23.  
  24. removeFile test1
  25. removeFile pipe
  26.  
  27. # set up a long data file for some of the following tests
  28.  
  29. set f [open longfile w]
  30. fconfigure $f -eofchar {} -translation lf
  31. for { set i 0 } { $i < 100 } { incr i} {
  32.     puts $f "#123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
  33. \#123456789abcdef01
  34. \#"
  35.     }
  36. close $f
  37.  
  38. set f [open cat w]
  39. puts $f {
  40.     if {$argv == {}} {
  41.     set argv -
  42.     }
  43.     foreach name $argv {
  44.     if {$name == "-"} {
  45.         set f stdin
  46.     } elseif {[catch {open $name r} f] != 0} {
  47.         puts stderr $f
  48.         continue
  49.     }
  50.     while {[eof $f] == 0} {
  51.         puts -nonewline stdout [read $f]
  52.     }
  53.     if {$f != "stdin"} {
  54.         close $f
  55.     }
  56.     }
  57. }
  58. close $f
  59.  
  60. # These tests are disabled until we decide what to do with "unsupported0".
  61. #
  62. #test io-1.7 {unsupported0 command} {
  63. #    removeFile test1
  64. #    set f1 [open iocmd.test]
  65. #    set f2 [open test1 w]
  66. #    unsupported0 $f1 $f2
  67. #    close $f1
  68. #    catch {close $f2}
  69. #    set s1 [file size [info script]]
  70. #    set s2 [file size test1]
  71. #    set x ok
  72. #    if {"$s1" != "$s2"} {
  73. #        set x broken
  74. #    }
  75. #    set x
  76. #} ok
  77. #test io-1.8 {unsupported0 command} {
  78. #    removeFile test1
  79. #    set f1 [open [info script]]
  80. #    set f2 [open test1 w]
  81. #    unsupported0 $f1 $f2 40
  82. #    close $f1
  83. #    close $f2
  84. #    file size test1
  85. #} 40
  86. #test io-1.9 {unsupported0 command} {
  87. #    removeFile test1
  88. #    set f1 [open [info script]]
  89. #    set f2 [open test1 w]
  90. #    unsupported0 $f1 $f2 -1
  91. #    close $f1
  92. #    close $f2
  93. #    set x ok
  94. #    set s1 [file size [info script]]
  95. #    set s2 [file size test1]
  96. #    if {$s1 != $s2} {
  97. #        set x broken
  98. #    }
  99. #    set x
  100. #} ok
  101. #test io-1.10 {unsupported0 command} {unixOrPc} {
  102. #    removeFile pipe
  103. #    removeFile test1
  104. #    set f1 [open pipe w]
  105. #    puts $f1 {puts ready}
  106. #    puts $f1 {gets stdin}
  107. #    puts $f1 {set f1 [open [info script] r]}
  108. #    puts $f1 {puts [read $f1 100]}
  109. #    puts $f1 {close $f1}
  110. #    close $f1
  111. #    set f1 [open "|[list $tcltest pipe]" r+]
  112. #    gets $f1
  113. #    puts $f1 ready
  114. #    flush $f1
  115. #    set f2 [open test1 w]
  116. #    set c [unsupported0 $f1 $f2 40]
  117. #    catch {close $f1}
  118. #    close $f2
  119. #    set s1 [file size test1]
  120. #    set x ok
  121. #    if {$s1 != "40"} {
  122. #        set x broken
  123. #    }
  124. #    list $c $x
  125. #} {40 ok}
  126.  
  127. # Test standard handle management. The functions tested are
  128. # Tcl_SetStdChannel and Tcl_GetStdChannel. Incidentally we are
  129. # also testing channel table management.
  130.  
  131. if {$tcl_platform(platform) == "macintosh"} {
  132.     set consoleFileNames [list console0 console1 console2]
  133. } else {
  134.     set consoleFileNames [lsort [testchannel open]]
  135. }
  136. test io-1.1 {Tcl_SetStdChannel and Tcl_GetStdChannel} {
  137.     set l ""
  138.     lappend l [fconfigure stdin -buffering]
  139.     lappend l [fconfigure stdout -buffering]
  140.     lappend l [fconfigure stderr -buffering]
  141.     lappend l [lsort [testchannel open]]
  142.     set l
  143. } [list line line none $consoleFileNames]
  144. test io-1.2 {Tcl_SetStdChannel and Tcl_GetStdChannel} {
  145.     interp create x
  146.     set l ""
  147.     lappend l [x eval {fconfigure stdin -buffering}]
  148.     lappend l [x eval {fconfigure stdout -buffering}]
  149.     lappend l [x eval {fconfigure stderr -buffering}]
  150.     interp delete x
  151.     set l
  152. } {line line none}
  153. test io-1.3 {Tcl_SetStdChannel & Tcl_GetStdChannel} {stdio} {
  154.     set f [open test1 w]
  155.     puts $f {
  156.     close stdin
  157.     close stdout
  158.     close stderr
  159.     set f [open test1 r]
  160.     set f2 [open test2 w]
  161.     set f3 [open test3 w]
  162.     puts stdout [gets stdin]
  163.     puts stdout out
  164.     puts stderr err
  165.     close $f
  166.     close $f2
  167.     close $f3
  168.     }
  169.     close $f
  170.     set result [exec $tcltest test1]
  171.     set f [open test2 r]
  172.     set f2 [open test3 r]
  173.     lappend result [read $f] [read $f2]
  174.     close $f
  175.     close $f2
  176.     set result
  177. } {{
  178. out
  179. } {err
  180. }}
  181. # This test relies on the fact that the smallest available fd is used first.
  182. test io-1.4 {Tcl_SetStdChannel & Tcl_GetStdChannel} {unixOnly} {
  183.     set f [open test1 w]
  184.     puts $f { close stdin
  185.     close stdout
  186.     close stderr
  187.     set f [open test1 r]
  188.     set f2 [open test2 w]
  189.     set f3 [open test3 w]
  190.     puts stdout [gets stdin]
  191.     puts stdout $f2
  192.     puts stderr $f3
  193.     close $f
  194.     close $f2
  195.     close $f3
  196.     }
  197.     close $f
  198.     set result [exec $tcltest test1]
  199.     set f [open test2 r]
  200.     set f2 [open test3 r]
  201.     lappend result [read $f] [read $f2]
  202.     close $f
  203.     close $f2
  204.     set result
  205. } {{ close stdin
  206. file1
  207. } {file2
  208. }}
  209. catch {interp delete z}
  210. test io-1.5 {Tcl_GetChannel: stdio name translation} {
  211.     interp create z
  212.     eof stdin
  213.     catch {z eval flush stdin} msg1
  214.     catch {z eval close stdin} msg2
  215.     catch {z eval flush stdin} msg3
  216.     set result [list $msg1 $msg2 $msg3]
  217.     interp delete z
  218.     set result
  219. } {{channel "stdin" wasn't opened for writing} {} {can not find channel named "stdin"}}
  220. test io-1.6 {Tcl_GetChannel: stdio name translation} {
  221.     interp create z
  222.     eof stdout
  223.     catch {z eval flush stdout} msg1
  224.     catch {z eval close stdout} msg2
  225.     catch {z eval flush stdout} msg3
  226.     set result [list $msg1 $msg2 $msg3]
  227.     interp delete z
  228.     set result
  229. } {{} {} {can not find channel named "stdout"}}
  230. test io-1.7 {Tcl_GetChannel: stdio name translation} {
  231.     interp create z
  232.     eof stderr
  233.     catch {z eval flush stderr} msg1
  234.     catch {z eval close stderr} msg2
  235.     catch {z eval flush stderr} msg3
  236.     set result [list $msg1 $msg2 $msg3]
  237.     interp delete z
  238.     set result
  239. } {{} {} {can not find channel named "stderr"}}
  240. test io-1.8 {reuse of stdio special channels} {unixOnly} {
  241.     removeFile script
  242.     removeFile test1
  243.     set f [open script w]
  244.     puts $f {
  245.     close stderr
  246.     set f [open test1 w]
  247.     puts stderr hello
  248.     close $f
  249.     set f [open test1 r]
  250.     puts [gets $f]
  251.     }
  252.     close $f
  253.     set f [open "|[list $tcltest script]" r]
  254.     set c [gets $f]
  255.     close $f
  256.     set c
  257. } hello
  258. test io-1.9 {reuse of stdio special channels} {stdio} {
  259.     removeFile script
  260.     removeFile test1
  261.     set f [open script w]
  262.     puts $f {
  263.     set f [open test1 w]
  264.     puts $f hello
  265.     close $f
  266.     close stderr
  267.     set f [open "|[list [info nameofexecutable] cat test1]" r]
  268.     puts [gets $f]
  269.     }
  270.     close $f
  271.     set f [open "|[list $tcltest script]" r]
  272.     set c [gets $f]
  273.     close $f
  274.     set c
  275. } hello
  276.  
  277. # Must add test function for testing Tcl_CreateCloseHandler and
  278. # Tcl_DeleteCloseHandler.
  279.  
  280. # Test channel table management. The functions tested are
  281. # GetChannelTable, DeleteChannelTable, Tcl_RegisterChannel,
  282. # Tcl_UnregisterChannel, Tcl_GetChannel and Tcl_CreateChannel.
  283. #
  284. # These functions use "eof stdin" to ensure that the standard
  285. # channels are added to the channel table of the interpreter.
  286.  
  287. test io-2.1 {GetChannelTable, DeleteChannelTable on std handles} {
  288.     set l1 [testchannel refcount stdin]
  289.     eof stdin
  290.     interp create x
  291.     set l ""
  292.     lappend l [expr [testchannel refcount stdin] - $l1]
  293.     x eval {eof stdin}
  294.     lappend l [expr [testchannel refcount stdin] - $l1]
  295.     interp delete x
  296.     lappend l [expr [testchannel refcount stdin] - $l1]
  297.     set l
  298. } {0 1 0}
  299. test io-2.2 {GetChannelTable, DeleteChannelTable on std handles} {
  300.     set l1 [testchannel refcount stdout]
  301.     eof stdin
  302.     interp create x
  303.     set l ""
  304.     lappend l [expr [testchannel refcount stdout] - $l1]
  305.     x eval {eof stdout}
  306.     lappend l [expr [testchannel refcount stdout] - $l1]
  307.     interp delete x
  308.     lappend l [expr [testchannel refcount stdout] - $l1]
  309.     set l
  310. } {0 1 0}
  311. test io-2.3 {GetChannelTable, DeleteChannelTable on std handles} {
  312.     set l1 [testchannel refcount stderr]
  313.     eof stdin
  314.     interp create x
  315.     set l ""
  316.     lappend l [expr [testchannel refcount stderr] - $l1]
  317.     x eval {eof stderr}
  318.     lappend l [expr [testchannel refcount stderr] - $l1]
  319.     interp delete x
  320.     lappend l [expr [testchannel refcount stderr] - $l1]
  321.     set l
  322. } {0 1 0}
  323. test io-2.4 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {
  324.     removeFile test1
  325.     set l ""
  326.     set f [open test1 w]
  327.     lappend l [lindex [testchannel info $f] 15]
  328.     close $f
  329.     if {[catch {lindex [testchannel info $f] 15} msg]} {
  330.     lappend l $msg
  331.     } else {
  332.     lappend l "very broken: $f found after being closed"
  333.     }
  334.     string compare [string tolower $l] \
  335.     [list 1 [format "can not find channel named \"%s\"" $f]]
  336. } 0
  337. test io-2.5 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {
  338.     removeFile test1
  339.     set l ""
  340.     set f [open test1 w]
  341.     lappend l [lindex [testchannel info $f] 15]
  342.     interp create x
  343.     interp share "" $f x
  344.     lappend l [lindex [testchannel info $f] 15]
  345.     x eval close $f
  346.     lappend l [lindex [testchannel info $f] 15]
  347.     interp delete x
  348.     lappend l [lindex [testchannel info $f] 15]
  349.     close $f
  350.     if {[catch {lindex [testchannel info $f] 15} msg]} {
  351.     lappend l $msg
  352.     } else {
  353.     lappend l "very broken: $f found after being closed"
  354.     }
  355.     string compare [string tolower $l] \
  356.     [list 1 2 1 1 [format "can not find channel named \"%s\"" $f]]
  357. } 0
  358. test io-2.6 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {
  359.     removeFile test1
  360.     set l ""
  361.     set f [open test1 w]
  362.     lappend l [lindex [testchannel info $f] 15]
  363.     interp create x
  364.     interp share "" $f x
  365.     lappend l [lindex [testchannel info $f] 15]
  366.     interp delete x
  367.     lappend l [lindex [testchannel info $f] 15]
  368.     close $f
  369.     if {[catch {lindex [testchannel info $f] 15} msg]} {
  370.     lappend l $msg
  371.     } else {
  372.     lappend l "very broken: $f found after being closed"
  373.     }
  374.     string compare [string tolower $l] \
  375.     [list 1 2 1 [format "can not find channel named \"%s\"" $f]]
  376. } 0
  377. test io-2.7 {Tcl_GetChannel->Tcl_GetStdChannel, standard handles} {
  378.     eof stdin
  379. } 0
  380. test io-2.8 {testing Tcl_GetChannel, user opened handle} {
  381.     removeFile test1
  382.     set f [open test1 w]
  383.     set x [eof $f]
  384.     close $f
  385.     set x
  386. } 0
  387. test io-2.9 {Tcl_GetChannel, channel not found} {
  388.     list [catch {eof file34} msg] $msg
  389. } {1 {can not find channel named "file34"}}
  390. test io-2.10 {Tcl_CreateChannel, insertion into channel table} {
  391.     removeFile test1
  392.     set f [open test1 w]
  393.     set l ""
  394.     lappend l [eof $f]
  395.     close $f
  396.     if {[catch {lindex [testchannel info $f] 15} msg]} {
  397.     lappend l $msg
  398.     } else {
  399.     lappend l "very broken: $f found after being closed"
  400.     }
  401.     string compare [string tolower $l] \
  402.     [list 0 [format "can not find channel named \"%s\"" $f]]
  403. } 0
  404.  
  405. # Test management of attributes associated with a channel, such as
  406. # its default translation, its name and type, etc. The functions
  407. # tested in this group are Tcl_GetChannelName,
  408. # Tcl_GetChannelType and Tcl_GetChannelFile. Tcl_GetChannelInstanceData
  409. # not tested because files do not use the instance data.
  410.  
  411. test io-3.1 {Tcl_GetChannelName} {
  412.     removeFile test1
  413.     set f [open test1 w]
  414.     set n [testchannel name $f]
  415.     close $f
  416.     string compare $n $f
  417. } 0
  418. test io-3.2 {Tcl_GetChannelType} {
  419.     removeFile test1
  420.     set f [open test1 w]
  421.     set t [testchannel type $f]
  422.     close $f
  423.     string compare $t file
  424. } 0
  425. test io-3.3 {Tcl_GetChannelFile, input} {
  426.     set f [open test1 w]
  427.     fconfigure $f -translation lf -eofchar {}
  428.     puts $f "1234567890\n098765432"
  429.     close $f
  430.     set f [open test1 r]
  431.     gets $f
  432.     set l ""
  433.     lappend l [testchannel inputbuffered $f]
  434.     lappend l [tell $f]
  435.     close $f
  436.     set l
  437. } {10 11}
  438. test io-3.4 {Tcl_GetChannelFile, output} {
  439.     removeFile test1
  440.     set f [open test1 w]
  441.     fconfigure $f -translation lf
  442.     puts $f hello
  443.     set l ""
  444.     lappend l [testchannel outputbuffered $f]
  445.     lappend l [tell $f]
  446.     flush $f
  447.     lappend l [testchannel outputbuffered $f]
  448.     lappend l [tell $f]
  449.     close $f
  450.     removeFile test1
  451.     set l
  452. } {6 6 0 6}
  453.  
  454. # Test flushing. The functions tested here are FlushChannel.
  455.  
  456. test io-4.1 {FlushChannel, no output buffered} {
  457.     removeFile test1
  458.     set f [open test1 w]
  459.     flush $f
  460.     set s [file size test1]
  461.     close $f
  462.     set s
  463. } 0
  464. test io-4.2 {FlushChannel, some output buffered} {
  465.     removeFile test1
  466.     set f [open test1 w]
  467.     fconfigure $f -translation lf -eofchar {}
  468.     set l ""
  469.     puts $f hello
  470.     lappend l [file size test1]
  471.     flush $f
  472.     lappend l [file size test1]
  473.     close $f
  474.     lappend l [file size test1]
  475.     set l
  476. } {0 6 6}
  477. test io-4.3 {FlushChannel, implicit flush on close} {
  478.     removeFile test1
  479.     set f [open test1 w]
  480.     fconfigure $f -translation lf -eofchar {}
  481.     set l ""
  482.     puts $f hello
  483.     lappend l [file size test1]
  484.     close $f
  485.     lappend l [file size test1]
  486.     set l
  487. } {0 6}
  488. test io-4.4 {FlushChannel, implicit flush when buffer fills} {
  489.     removeFile test1
  490.     set f [open test1 w]
  491.     fconfigure $f -translation lf -eofchar {}
  492.     fconfigure $f -buffersize 60
  493.     set l ""
  494.     lappend l [file size test1]
  495.     for {set i 0} {$i < 12} {incr i} {
  496.     puts $f hello
  497.     }
  498.     lappend l [file size test1]
  499.     flush $f
  500.     lappend l [file size test1]
  501.     close $f
  502.     set l
  503. } {0 60 72}
  504. test io-4.5 {FlushChannel, implicit flush when buffer fills and on close} {unixOrPc} {
  505.     removeFile test1
  506.     set f [open test1 w]
  507.     fconfigure $f -translation lf -buffersize 60 -eofchar {}
  508.     set l ""
  509.     lappend l [file size test1]
  510.     for {set i 0} {$i < 12} {incr i} {
  511.     puts $f hello
  512.     }
  513.     lappend l [file size test1]
  514.     close $f
  515.     lappend l [file size test1]
  516.     set l
  517. } {0 60 72}
  518. test io-4.6 {FlushChannel, async flushing, async close} {stdio && asyncPipeClose} {
  519.     removeFile pipe
  520.     removeFile output
  521.     set f [open pipe w]
  522.     puts $f {
  523.     set f [open output w]
  524.     fconfigure $f -translation lf -buffering none -eofchar {}
  525.     while {![eof stdin]} {
  526.         after 20
  527.         puts -nonewline $f [read stdin 1024]
  528.     }
  529.     close $f
  530.     }
  531.     close $f
  532.     set x 01234567890123456789012345678901
  533.     for {set i 0} {$i < 11} {incr i} {
  534.         set x "$x$x"
  535.     }
  536.     set f [open output w]
  537.     close $f
  538.     set f [open "|[list $tcltest pipe]" w]
  539.     fconfigure $f -blocking off
  540.     puts -nonewline $f $x
  541.     close $f
  542.     set counter 0
  543.     while {([file size output] < 65536) && ($counter < 1000)} {
  544.         incr counter
  545.         after 20
  546.         update
  547.     }
  548.     if {$counter == 1000} {
  549.         set result probably_broken
  550.     } else {
  551.         set result ok
  552.     }
  553. } ok
  554.  
  555. # Tests closing a channel. The functions tested are CloseChannel and Tcl_Close.
  556.  
  557. test io-5.1 {CloseChannel called when all references are dropped} {
  558.     removeFile test1
  559.     set f [open test1 w]
  560.     interp create x
  561.     interp share "" $f x
  562.     set l ""
  563.     lappend l [testchannel refcount $f]
  564.     x eval close $f
  565.     interp delete x
  566.     lappend l [testchannel refcount $f]
  567.     close $f
  568.     set l
  569. } {2 1}
  570. test io-5.2 {CloseChannel called when all references are dropped} {
  571.     removeFile test1
  572.     set f [open test1 w]
  573.     interp create x
  574.     interp share "" $f x
  575.     puts -nonewline $f abc
  576.     close $f
  577.     x eval puts $f def
  578.     x eval close $f
  579.     interp delete x
  580.     set f [open test1 r]
  581.     set l [gets $f]
  582.     close $f
  583.     set l
  584. } abcdef
  585. test io-5.3 {CloseChannel, not called before output queue is empty} {unixOrPc asyncPipeClose nonPortable tempNotPc} {
  586.     removeFile pipe
  587.     removeFile output
  588.     set f [open pipe w]
  589.     puts $f {
  590.  
  591.     # Need to not have eof char appended on close, because the other
  592.     # side of the pipe already closed, so that writing would cause an
  593.     # error "invalid file".
  594.  
  595.     fconfigure stdout -eofchar {}
  596.     fconfigure stderr -eofchar {}
  597.  
  598.     set f [open output w]
  599.     fconfigure $f -translation lf -buffering none
  600.     for {set x 0} {$x < 20} {incr x} {
  601.         after 20
  602.         puts -nonewline $f [read stdin 1024]
  603.     }
  604.     close $f
  605.     }
  606.     close $f
  607.     set x 01234567890123456789012345678901
  608.     for {set i 0} {$i < 11} {incr i} {
  609.         set x "$x$x"
  610.     }
  611.     set f [open output w]
  612.     close $f
  613.     set f [open "|[list $tcltest pipe]" r+]
  614.     fconfigure $f -blocking off -eofchar {}
  615.  
  616.     # Under windows, the first 24576 bytes of $x are copied to $f, and
  617.     # then the writing fails.  
  618.  
  619.     puts -nonewline $f $x
  620.     close $f
  621.     set counter 0
  622.     while {([file size output] < 20480) && ($counter < 1000)} {
  623.         incr counter
  624.         after 20
  625.         update
  626.     }
  627.     if {$counter == 1000} {
  628.         set result probably_broken
  629.     } else {
  630.         set result ok
  631.     }
  632. } ok
  633. test io-5.4 {Tcl_Close} {
  634.     removeFile test1
  635.     set l ""
  636.     lappend l [lsort [testchannel open]]
  637.     set f [open test1 w]
  638.     lappend l [lsort [testchannel open]]
  639.     close $f
  640.     lappend l [lsort [testchannel open]]
  641.     set x [list $consoleFileNames \
  642.         [lsort [eval list $consoleFileNames $f]] \
  643.         $consoleFileNames]
  644.     string compare $l $x
  645. } 0
  646. test io-5.5 {Tcl_Close vs standard handles} {unixOnly} {
  647.     removeFile script
  648.     set f [open script w]
  649.     puts $f {
  650.     close stdin
  651.     puts [testchannel open]
  652.     }
  653.     close $f
  654.     set f [open "|[list $tcltest script]" r]
  655.     set l [gets $f]
  656.     close $f
  657.     set l
  658. } {file1 file2}
  659.  
  660. # Test output on channels. The functions tested are Tcl_Write
  661. # and Tcl_Flush.
  662.  
  663. test io-6.1 {Tcl_Write, channel not writable} {
  664.     list [catch {puts stdin hello} msg] $msg
  665. } {1 {channel "stdin" wasn't opened for writing}}
  666. test io-6.2 {Tcl_Write, empty string} {
  667.     removeFile test1
  668.     set f [open test1 w]
  669.     fconfigure $f -eofchar {}
  670.     puts -nonewline $f ""
  671.     close $f
  672.     file size test1
  673. } 0
  674. test io-6.3 {Tcl_Write, nonempty string} {
  675.     removeFile test1
  676.     set f [open test1 w]
  677.     fconfigure $f -eofchar {}
  678.     puts -nonewline $f hello
  679.     close $f
  680.     file size test1
  681. } 5
  682. test io-6.4 {Tcl_Write, buffering in full buffering mode} {
  683.     removeFile test1
  684.     set f [open test1 w]
  685.     fconfigure $f -translation lf -buffering full -eofchar {}
  686.     puts $f hello
  687.     set l ""
  688.     lappend l [testchannel outputbuffered $f]
  689.     lappend l [file size test1]
  690.     flush $f
  691.     lappend l [testchannel outputbuffered $f]
  692.     lappend l [file size test1]
  693.     close $f
  694.     set l
  695. } {6 0 0 6}
  696. test io-6.5 {Tcl_Write, buffering in line buffering mode} {
  697.     removeFile test1
  698.     set f [open test1 w]
  699.     fconfigure $f -translation lf -buffering line -eofchar {}
  700.     puts -nonewline $f hello
  701.     set l ""
  702.     lappend l [testchannel outputbuffered $f]
  703.     lappend l [file size test1]
  704.     puts $f hello
  705.     lappend l [testchannel outputbuffered $f]
  706.     lappend l [file size test1]
  707.     close $f
  708.     set l
  709. } {5 0 0 11}
  710. test io-6.6 {Tcl_Write, buffering in no buffering mode} {
  711.     removeFile test1
  712.     set f [open test1 w]
  713.     fconfigure $f -translation lf -buffering none -eofchar {}
  714.     puts -nonewline $f hello
  715.     set l ""
  716.     lappend l [testchannel outputbuffered $f]
  717.     lappend l [file size test1]
  718.     puts $f hello
  719.     lappend l [testchannel outputbuffered $f]
  720.     lappend l [file size test1]
  721.     close $f
  722.     set l
  723. } {0 5 0 11}
  724. test io-6.7 {Tcl_Flush, full buffering} {
  725.     removeFile test1
  726.     set f [open test1 w]
  727.     fconfigure $f -translation lf -buffering full -eofchar {}
  728.     puts -nonewline $f hello
  729.     set l ""
  730.     lappend l [testchannel outputbuffered $f]
  731.     lappend l [file size test1]
  732.     puts $f hello
  733.     lappend l [testchannel outputbuffered $f]
  734.     lappend l [file size test1]
  735.     flush $f
  736.     lappend l [testchannel outputbuffered $f]
  737.     lappend l [file size test1]
  738.     close $f
  739.     set l
  740. } {5 0 11 0 0 11}
  741. test io-6.8 {Tcl_Flush, full buffering} {
  742.     removeFile test1
  743.     set f [open test1 w]
  744.     fconfigure $f -translation lf -buffering line
  745.     puts -nonewline $f hello
  746.     set l ""
  747.     lappend l [testchannel outputbuffered $f]
  748.     lappend l [file size test1]
  749.     flush $f
  750.     lappend l [testchannel outputbuffered $f]
  751.     lappend l [file size test1]
  752.     puts $f hello
  753.     lappend l [testchannel outputbuffered $f]
  754.     lappend l [file size test1]
  755.     flush $f
  756.     lappend l [testchannel outputbuffered $f]
  757.     lappend l [file size test1]
  758.     close $f
  759.     set l
  760. } {5 0 0 5 0 11 0 11}
  761. test io-6.9 {Tcl_Flush, channel not writable} {
  762.     list [catch {flush stdin} msg] $msg
  763. } {1 {channel "stdin" wasn't opened for writing}}
  764. test io-6.10 {Tcl_Write, looping and buffering} {
  765.     removeFile test1
  766.     set f1 [open test1 w]
  767.     fconfigure $f1 -translation lf -eofchar {}
  768.     set f2 [open longfile r]
  769.     for {set x 0} {$x < 10} {incr x} {
  770.         puts $f1 [gets $f2]
  771.     }
  772.     close $f2
  773.     close $f1
  774.     file size test1
  775. } 387
  776. test io-6.11 {Tcl_Write, no newline, implicit flush} {
  777.     removeFile test1
  778.     set f1 [open test1 w]
  779.     fconfigure $f1 -eofchar {}
  780.     set f2 [open longfile r]
  781.     for {set x 0} {$x < 10} {incr x} {
  782.         puts -nonewline $f1 [gets $f2]
  783.     }
  784.     close $f1
  785.     close $f2
  786.     file size test1
  787. } 377
  788. test io-6.12 {Tcl_Write on a pipe} {stdio} {
  789.     removeFile test1
  790.     removeFile pipe
  791.     set f1 [open pipe w]
  792.     puts $f1 {
  793.     set f1 [open longfile r]
  794.     for {set x 0} {$x < 10} {incr x} {
  795.         puts [gets $f1]
  796.     }
  797.     }
  798.     close $f1
  799.     set f1 [open "|[list $tcltest pipe]" r]
  800.     set f2 [open longfile r]
  801.     set y ok
  802.     for {set x 0} {$x < 10} {incr x} {
  803.         set l1 [gets $f1]
  804.         set l2 [gets $f2]
  805.         if {"$l1" != "$l2"} {
  806.             set y broken
  807.         }
  808.     }
  809.     close $f1
  810.     close $f2
  811.     set y
  812. } ok
  813. test io-6.13 {Tcl_Write to a pipe, line buffered} {stdio} {
  814.     removeFile test1
  815.     removeFile pipe
  816.     set f1 [open pipe w]
  817.     puts $f1 {
  818.     puts [gets stdin]
  819.     puts [gets stdin]
  820.     }
  821.     close $f1
  822.     set y ok
  823.     set f1 [open "|[list $tcltest pipe]" r+]
  824.     fconfigure $f1 -buffering line
  825.     set f2 [open longfile r]
  826.     set line [gets $f2]
  827.     puts $f1 $line
  828.     set backline [gets $f1]
  829.     if {"$line" != "$backline"} {
  830.         set y broken
  831.     }
  832.     set line [gets $f2]
  833.     puts $f1 $line
  834.     set backline [gets $f1]
  835.     if {"$line" != "$backline"} {
  836.         set y broken
  837.     }
  838.     close $f1
  839.     close $f2
  840.     set y
  841. } ok
  842. test io-6.14 {Tcl_Write, buffering and implicit flush at close} {
  843.     removeFile test3
  844.     set f [open test3 w]
  845.     puts -nonewline $f "Text1"
  846.     puts -nonewline $f " Text 2"
  847.     puts $f " Text 3"
  848.     close $f
  849.     set f [open test3 r]
  850.     set x [gets $f]
  851.     close $f
  852.     set x
  853. } {Text1 Text 2 Text 3}
  854. test io-6.15 {Tcl_Flush, channel not open for writing} {
  855.     removeFile test1
  856.     set fd [open test1 w]
  857.     close $fd
  858.     set fd [open test1 r]
  859.     set x [list [catch {flush $fd} msg] $msg]
  860.     close $fd
  861.     string compare $x \
  862.     [list 1 "channel \"$fd\" wasn't opened for writing"]
  863. } 0
  864. test io-6.16 {Tcl_Flush on pipe opened only for reading} {stdio} {
  865.     set fd [open "|[list $tcltest cat longfile]" r]
  866.     set x [list [catch {flush $fd} msg] $msg]
  867.     catch {close $fd}
  868.     string compare $x \
  869.     [list 1 "channel \"$fd\" wasn't opened for writing"]
  870. } 0
  871. test io-6.17 {Tcl_Write buffers, then Tcl_Flush flushes} {
  872.     removeFile test1
  873.     set f1 [open test1 w]
  874.     fconfigure $f1 -translation lf
  875.     puts $f1 hello
  876.     puts $f1 hello
  877.     puts $f1 hello
  878.     flush $f1
  879.     set x [file size test1]
  880.     close $f1
  881.     set x
  882. } 18
  883. test io-6.18 {Tcl_Write and Tcl_Flush intermixed} {
  884.     removeFile test1
  885.     set x ""
  886.     set f1 [open test1 w]
  887.     fconfigure $f1 -translation lf
  888.     puts $f1 hello
  889.     puts $f1 hello
  890.     puts $f1 hello
  891.     flush $f1
  892.     lappend x [file size test1]
  893.     puts $f1 hello
  894.     flush $f1
  895.     lappend x [file size test1]
  896.     puts $f1 hello
  897.     flush $f1
  898.     lappend x [file size test1]
  899.     close $f1
  900.     set x
  901. } {18 24 30}
  902. test io-6.19 {Explicit and implicit flushes} {
  903.     removeFile test1
  904.     set f1 [open test1 w]
  905.     fconfigure $f1 -translation lf -eofchar {}
  906.     set x ""
  907.     puts $f1 hello
  908.     puts $f1 hello
  909.     puts $f1 hello
  910.     flush $f1
  911.     lappend x [file size test1]
  912.     puts $f1 hello
  913.     flush $f1
  914.     lappend x [file size test1]
  915.     puts $f1 hello
  916.     close $f1
  917.     lappend x [file size test1]
  918.     set x
  919. } {18 24 30}
  920. test io-6.20 {Implicit flush when buffer is full} {
  921.     removeFile test1
  922.     set f1 [open test1 w]
  923.     fconfigure $f1 -translation lf -eofchar {}
  924.     set line "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  925.     for {set x 0} {$x < 100} {incr x} {
  926.       puts $f1 $line
  927.     }
  928.     set z ""
  929.     lappend z [file size test1]
  930.     for {set x 0} {$x < 100} {incr x} {
  931.         puts $f1 $line
  932.     }
  933.     lappend z [file size test1]
  934.     close $f1
  935.     lappend z [file size test1]
  936.     set z
  937. } {4096 12288 12600}
  938. test io-6.21 {Tcl_Flush to pipe} {stdio} {
  939.     removeFile pipe
  940.     set f1 [open pipe w]
  941.     puts $f1 {set x [read stdin 6]}
  942.     puts $f1 {set cnt [string length $x]}
  943.     puts $f1 {puts "read $cnt characters"}
  944.     close $f1
  945.     set f1 [open "|[list $tcltest pipe]" r+]
  946.     puts $f1 hello
  947.     flush $f1
  948.     set x [gets $f1]
  949.     catch {close $f1}
  950.     set x
  951. } "read 6 characters"
  952. test io-6.22 {Tcl_Flush called at other end of pipe} {stdio} {
  953.     removeFile pipe
  954.     set f1 [open pipe w]
  955.     puts $f1 {
  956.     fconfigure stdout -buffering full
  957.     puts hello
  958.     puts hello
  959.     flush stdout
  960.     gets stdin
  961.     puts bye
  962.     flush stdout
  963.     }
  964.     close $f1
  965.     set f1 [open "|[list $tcltest pipe]" r+]
  966.     set x ""
  967.     lappend x [gets $f1]
  968.     lappend x [gets $f1]
  969.     puts $f1 hello
  970.     flush $f1
  971.     lappend x [gets $f1]
  972.     close $f1
  973.     set x
  974. } {hello hello bye}
  975. test io-6.23 {Tcl_Flush and line buffering at end of pipe} {stdio} {
  976.     removeFile pipe
  977.     set f1 [open pipe w]
  978.     puts $f1 {
  979.     puts hello
  980.     puts hello
  981.     gets stdin
  982.     puts bye
  983.     }
  984.     close $f1
  985.     set f1 [open "|[list $tcltest pipe]" r+]
  986.     set x ""
  987.     lappend x [gets $f1]
  988.     lappend x [gets $f1]
  989.     puts $f1 hello
  990.     flush $f1
  991.     lappend x [gets $f1]
  992.     close $f1
  993.     set x
  994. } {hello hello bye}
  995. test io-6.24 {Tcl_Write and Tcl_Flush move end of file} {
  996.     set f [open test3 w]
  997.     puts $f "Line 1"
  998.     puts $f "Line 2"
  999.     set f2 [open test3]
  1000.     set x {}
  1001.     lappend x [read -nonewline $f2]
  1002.     close $f2
  1003.     flush $f
  1004.     set f2 [open test3]
  1005.     lappend x [read -nonewline $f2]
  1006.     close $f2
  1007.     close $f
  1008.     set x
  1009. } {{} {Line 1
  1010. Line 2}}
  1011. test io-6.25 {Implicit flush with Tcl_Flush to command pipelines} {stdio} {
  1012.     removeFile test3
  1013.     set f [open "|[list $tcltest cat | $tcltest cat > test3]" w]
  1014.     puts $f "Line 1"
  1015.     puts $f "Line 2"
  1016.     close $f
  1017.     after 100
  1018.     set f [open test3 r]
  1019.     set x [read $f]
  1020.     close $f
  1021.     set x
  1022. } {Line 1
  1023. Line 2
  1024. }    
  1025. test io-6.26 {Tcl_Flush, Tcl_Write on bidirectional pipelines} {unixOrPc && unixExecs && tempNotPc} {
  1026.     set f [open "|[list cat -u]" r+]
  1027.     puts $f "Line1"
  1028.     flush $f
  1029.     set x [gets $f]
  1030.     close $f
  1031.     set x
  1032. } {Line1}
  1033. test io-6.27 {Tcl_Flush on closed pipeline} {stdio && tempNotPc} {
  1034.     removeFile pipe
  1035.     set f [open pipe w]
  1036.     puts $f {exit}
  1037.     close $f
  1038.     set f [open "|[list $tcltest pipe]" r+]
  1039.     gets $f
  1040.     puts $f output
  1041.     after 50
  1042.     #
  1043.     # The flush below will get a SIGPIPE. This is an expected part of
  1044.     # test and indicates that the test operates correctly. If you run
  1045.     # this test under a debugger, the signal will by intercepted unless
  1046.     # you disable the debugger's signal interception.
  1047.     #
  1048.     if {[catch {flush $f} msg]} {
  1049.     set x [list 1 $msg $errorCode]
  1050.     catch {close $f}
  1051.     } else {
  1052.     if {[catch {close $f} msg]} {
  1053.         set x [list 1 $msg $errorCode]
  1054.     } else {
  1055.         set x {this was supposed to fail and did not}
  1056.     }
  1057.     }
  1058.     regsub {".*":} $x {"":} x
  1059.     string tolower $x
  1060. } {1 {error flushing "": broken pipe} {posix epipe {broken pipe}}}
  1061. test io-6.28 {Tcl_Write, lf mode} {
  1062.     removeFile test1
  1063.     set f [open test1 w]
  1064.     fconfigure $f -translation lf -eofchar {}
  1065.     puts $f hello\nthere\nand\nhere
  1066.     flush $f
  1067.     set s [file size test1]
  1068.     close $f
  1069.     set s
  1070. } 21
  1071. test io-6.29 {Tcl_Write, cr mode} {
  1072.     removeFile test1
  1073.     set f [open test1 w]
  1074.     fconfigure $f -translation cr -eofchar {}
  1075.     puts $f hello\nthere\nand\nhere
  1076.     close $f
  1077.     file size test1
  1078. } 21
  1079. test io-6.30 {Tcl_Write, crlf mode} {
  1080.     removeFile test1
  1081.     set f [open test1 w]
  1082.     fconfigure $f -translation crlf -eofchar {}
  1083.     puts $f hello\nthere\nand\nhere
  1084.     close $f
  1085.     file size test1
  1086. } 25
  1087. test io-6.31 {Tcl_Write, background flush} {stdio} {
  1088.     removeFile pipe
  1089.     removeFile output
  1090.     set f [open pipe w]
  1091.     puts $f {set f [open output w]}
  1092.     puts $f {fconfigure $f -translation lf}
  1093.     set x [list while {![eof stdin]}]
  1094.     set x "$x {"
  1095.     puts $f $x
  1096.     puts $f {  puts -nonewline $f [read stdin 4096]}
  1097.     puts $f {  flush $f}
  1098.     puts $f "}"
  1099.     puts $f {close $f}
  1100.     close $f
  1101.     set x 01234567890123456789012345678901
  1102.     for {set i 0} {$i < 11} {incr i} {
  1103.         set x "$x$x"
  1104.     }
  1105.     set f [open output w]
  1106.     close $f
  1107.     set f [open "|[list $tcltest pipe]" r+]
  1108.     fconfigure $f -blocking off
  1109.     puts -nonewline $f $x
  1110.     close $f
  1111.     set counter 0
  1112.     while {([file size output] < 65536) && ($counter < 1000)} {
  1113.         incr counter
  1114.         after 5
  1115.         update
  1116.     }
  1117.     if {$counter == 1000} {
  1118.         set result probably_broken
  1119.     } else {
  1120.         set result ok
  1121.     }
  1122. } ok
  1123. test io-6.32 {Tcl_Write, background flush to slow reader} {stdio && asyncPipeClose} {
  1124.     removeFile pipe
  1125.     removeFile output
  1126.     set f [open pipe w]
  1127.     puts $f {set f [open output w]}
  1128.     puts $f {fconfigure $f -translation lf}
  1129.     set x [list while {![eof stdin]}]
  1130.     set x "$x {"
  1131.     puts $f $x
  1132.     puts $f {  after 20}
  1133.     puts $f {  puts -nonewline $f [read stdin 1024]}
  1134.     puts $f {  flush $f}
  1135.     puts $f "}"
  1136.     puts $f {close $f}
  1137.     close $f
  1138.     set x 01234567890123456789012345678901
  1139.     for {set i 0} {$i < 11} {incr i} {
  1140.         set x "$x$x"
  1141.     }
  1142.     set f [open output w]
  1143.     close $f
  1144.     set f [open "|[list $tcltest pipe]" r+]
  1145.     fconfigure $f -blocking off
  1146.     puts -nonewline $f $x
  1147.     close $f
  1148.     set counter 0
  1149.     while {([file size output] < 65536) && ($counter < 1000)} {
  1150.         incr counter
  1151.         after 20
  1152.         update
  1153.     }
  1154.     if {$counter == 1000} {
  1155.         set result probably_broken
  1156.     } else {
  1157.         set result ok
  1158.     }
  1159. } ok
  1160. test io-6.33 {Tcl_Flush, implicit flush on exit} {stdio} {
  1161.     set f [open script w]
  1162.     puts $f {
  1163.     set f [open test1 w]
  1164.     fconfigure $f -translation lf
  1165.     puts $f hello
  1166.     puts $f bye
  1167.     puts $f strange
  1168.     }
  1169.     close $f
  1170.     exec $tcltest script
  1171.     set f [open test1 r]
  1172.     set r [read $f]
  1173.     close $f
  1174.     set r
  1175. } {hello
  1176. bye
  1177. strange
  1178. }
  1179.  
  1180. test io-6.34 {Tcl_Close, async flush on close, using sockets} {socket tempNotMac} {
  1181.     set c 0
  1182.     set x running
  1183.     set l abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
  1184.     proc writelots {s l} {
  1185.     for {set i 0} {$i < 2000} {incr i} {
  1186.         puts $s $l
  1187.     }
  1188.     }
  1189.     proc accept {s a p} {
  1190.     global x
  1191.     fileevent $s readable [list readit $s]
  1192.     fconfigure $s -blocking off
  1193.     set x accepted
  1194.     }
  1195.     proc readit {s} {
  1196.     global c x
  1197.     set l [gets $s]
  1198.     
  1199.     if {[eof $s]} {
  1200.         close $s
  1201.         set x done
  1202.     } elseif {([string length $l] > 0) || ![fblocked $s]} {
  1203.         incr c
  1204.     }
  1205.     }
  1206.     set ss [socket -server accept 2828]
  1207.     set cs [socket [info hostname] 2828]
  1208.     vwait x
  1209.     fconfigure $cs -blocking off
  1210.     writelots $cs $l
  1211.     close $cs
  1212.     close $ss
  1213.     vwait x
  1214.     set c
  1215. } 2000
  1216. test io-6.35 {Tcl_Close vs fileevent vs multiple interpreters} {socket} {
  1217.     catch {interp delete x}
  1218.     catch {interp delete y}
  1219.     interp create x
  1220.     interp create y
  1221.     set s [socket -server accept 2828]
  1222.     proc accept {s a p} {
  1223.     puts $s hello
  1224.     close $s
  1225.     }
  1226.     set c [socket [info hostname] 2828]
  1227.     interp share {} $c x
  1228.     interp share {} $c y
  1229.     close $c
  1230.     x eval {
  1231.     proc readit {s} {
  1232.         gets $s
  1233.         if {[eof $s]} {
  1234.         close $s
  1235.         }
  1236.     }
  1237.     }
  1238.     y eval {
  1239.     proc readit {s} {
  1240.         gets $s
  1241.         if {[eof $s]} {
  1242.         close $s
  1243.         }
  1244.     }
  1245.     }
  1246.     x eval "fileevent $c readable \{readit $c\}"
  1247.     y eval "fileevent $c readable \{readit $c\}"
  1248.     y eval [list close $c]
  1249.     update
  1250.     close $s
  1251.     interp delete x
  1252.     interp delete y
  1253. } ""
  1254.  
  1255. # Test end of line translations. Procedures tested are Tcl_Write, Tcl_Read.
  1256.  
  1257. test io-7.1 {Tcl_Write lf, Tcl_Read lf} {
  1258.     removeFile test1
  1259.     set f [open test1 w]
  1260.     fconfigure $f -translation lf
  1261.     puts $f hello\nthere\nand\nhere
  1262.     close $f
  1263.     set f [open test1 r]
  1264.     fconfigure $f -translation lf
  1265.     set x [read $f]
  1266.     close $f
  1267.     set x
  1268. } "hello\nthere\nand\nhere\n"
  1269. test io-7.2 {Tcl_Write lf, Tcl_Read cr} {
  1270.     removeFile test1
  1271.     set f [open test1 w]
  1272.     fconfigure $f -translation lf
  1273.     puts $f hello\nthere\nand\nhere
  1274.     close $f
  1275.     set f [open test1 r]
  1276.     fconfigure $f -translation cr
  1277.     set x [read $f]
  1278.     close $f
  1279.     set x
  1280. } "hello\nthere\nand\nhere\n"
  1281. test io-7.3 {Tcl_Write lf, Tcl_Read crlf} {
  1282.     removeFile test1
  1283.     set f [open test1 w]
  1284.     fconfigure $f -translation lf
  1285.     puts $f hello\nthere\nand\nhere
  1286.     close $f
  1287.     set f [open test1 r]
  1288.     fconfigure $f -translation crlf
  1289.     set x [read $f]
  1290.     close $f
  1291.     set x
  1292. } "hello\nthere\nand\nhere\n"
  1293. test io-7.4 {Tcl_Write cr, Tcl_Read cr} {
  1294.     removeFile test1
  1295.     set f [open test1 w]
  1296.     fconfigure $f -translation cr
  1297.     puts $f hello\nthere\nand\nhere
  1298.     close $f
  1299.     set f [open test1 r]
  1300.     fconfigure $f -translation cr
  1301.     set x [read $f]
  1302.     close $f
  1303.     set x
  1304. } "hello\nthere\nand\nhere\n"
  1305. test io-7.5 {Tcl_Write cr, Tcl_Read lf} {
  1306.     removeFile test1
  1307.     set f [open test1 w]
  1308.     fconfigure $f -translation cr
  1309.     puts $f hello\nthere\nand\nhere
  1310.     close $f
  1311.     set f [open test1 r]
  1312.     fconfigure $f -translation lf
  1313.     set x [read $f]
  1314.     close $f
  1315.     set x
  1316. } "hello\rthere\rand\rhere\r"
  1317. test io-7.6 {Tcl_Write cr, Tcl_Read crlf} {
  1318.     removeFile test1
  1319.     set f [open test1 w]
  1320.     fconfigure $f -translation cr
  1321.     puts $f hello\nthere\nand\nhere
  1322.     close $f
  1323.     set f [open test1 r]
  1324.     fconfigure $f -translation crlf
  1325.     set x [read $f]
  1326.     close $f
  1327.     set x 
  1328. } "hello\rthere\rand\rhere\r"
  1329. test io-7.7 {Tcl_Write crlf, Tcl_Read crlf} {
  1330.     removeFile test1
  1331.     set f [open test1 w]
  1332.     fconfigure $f -translation crlf
  1333.     puts $f hello\nthere\nand\nhere
  1334.     close $f
  1335.     set f [open test1 r]
  1336.     fconfigure $f -translation crlf
  1337.     set x [read $f]
  1338.     close $f
  1339.     set x
  1340. } "hello\nthere\nand\nhere\n"
  1341. test io-7.8 {Tcl_Write crlf, Tcl_Read lf} {
  1342.     removeFile test1
  1343.     set f [open test1 w]
  1344.     fconfigure $f -translation crlf
  1345.     puts $f hello\nthere\nand\nhere
  1346.     close $f
  1347.     set f [open test1 r]
  1348.     fconfigure $f -translation lf
  1349.     set x [read $f]
  1350.     close $f
  1351.     set x
  1352. } "hello\r\nthere\r\nand\r\nhere\r\n"
  1353. test io-7.9 {Tcl_Write crlf, Tcl_Read cr} {
  1354.     removeFile test1
  1355.     set f [open test1 w]
  1356.     fconfigure $f -translation crlf
  1357.     puts $f hello\nthere\nand\nhere
  1358.     close $f
  1359.     set f [open test1 r]
  1360.     fconfigure $f -translation cr
  1361.     set x [read $f]
  1362.     close $f
  1363.     set x
  1364. } "hello\n\nthere\n\nand\n\nhere\n\n"
  1365. test io-7.10 {Tcl_Write lf, Tcl_Read auto} {
  1366.     removeFile test1
  1367.     set f [open test1 w]
  1368.     fconfigure $f -translation lf
  1369.     puts $f hello\nthere\nand\nhere
  1370.     close $f
  1371.     set f [open test1 r]
  1372.     set c [read $f]
  1373.     set x [fconfigure $f -translation]
  1374.     close $f
  1375.     list $c $x
  1376. } {{hello
  1377. there
  1378. and
  1379. here
  1380. } auto}
  1381. test io-7.11 {Tcl_Write cr, Tcl_Read auto} {
  1382.     removeFile test1
  1383.     set f [open test1 w]
  1384.     fconfigure $f -translation cr
  1385.     puts $f hello\nthere\nand\nhere
  1386.     close $f
  1387.     set f [open test1 r]
  1388.     set c [read $f]
  1389.     set x [fconfigure $f -translation]
  1390.     close $f
  1391.     list $c $x
  1392. } {{hello
  1393. there
  1394. and
  1395. here
  1396. } auto}
  1397. test io-7.12 {Tcl_Write crlf, Tcl_Read auto} {
  1398.     removeFile test1
  1399.     set f [open test1 w]
  1400.     fconfigure $f -translation crlf
  1401.     puts $f hello\nthere\nand\nhere
  1402.     close $f
  1403.     set f [open test1 r]
  1404.     set c [read $f]
  1405.     set x [fconfigure $f -translation]
  1406.     close $f
  1407.     list $c $x
  1408. } {{hello
  1409. there
  1410. and
  1411. here
  1412. } auto}
  1413.  
  1414. test io-7.13 {Tcl_Write crlf on block boundary, Tcl_Read auto} {
  1415.     removeFile test1
  1416.     set f [open test1 w]
  1417.     fconfigure $f -translation crlf
  1418.     set line "123456789ABCDE"    ;# 14 char plus crlf
  1419.     puts -nonewline $f x    ;# shift crlf across block boundary
  1420.     for {set i 0} {$i < 700} {incr i} {
  1421.     puts $f $line
  1422.     }
  1423.     close $f
  1424.     set f [open test1 r]
  1425.     fconfigure $f -translation auto
  1426.     set c [read $f]
  1427.     close $f
  1428.     string length $c
  1429. } [expr 700*15+1]
  1430.  
  1431. test io-7.14 {Tcl_Write crlf on block boundary, Tcl_Read crlf} {
  1432.     removeFile test1
  1433.     set f [open test1 w]
  1434.     fconfigure $f -translation crlf
  1435.     set line "123456789ABCDE"    ;# 14 char plus crlf
  1436.     puts -nonewline $f x    ;# shift crlf across block boundary
  1437.     for {set i 0} {$i < 700} {incr i} {
  1438.     puts $f $line
  1439.     }
  1440.     close $f
  1441.     set f [open test1 r]
  1442.     fconfigure $f -translation crlf
  1443.     set c [read $f]
  1444.     close $f
  1445.     string length $c
  1446. } [expr 700*15+1]
  1447.  
  1448. test io-7.15 {Tcl_Write mixed, Tcl_Read auto} {
  1449.     removeFile test1
  1450.     set f [open test1 w]
  1451.     fconfigure $f -translation lf
  1452.     puts $f hello\nthere\nand\rhere
  1453.     close $f
  1454.     set f [open test1 r]
  1455.     fconfigure $f -translation auto
  1456.     set c [read $f]
  1457.     close $f
  1458.     set c
  1459. } {hello
  1460. there
  1461. and
  1462. here
  1463. }
  1464. test io-7.16 {Tcl_Write ^Z at end, Tcl_Read auto} {
  1465.     removeFile test1
  1466.     set f [open test1 w]
  1467.     fconfigure $f -translation lf
  1468.     puts -nonewline $f hello\nthere\nand\rhere\n\x1a
  1469.     close $f
  1470.     set f [open test1 r]
  1471.     fconfigure $f -eofchar \x1a -translation auto
  1472.     set c [read $f]
  1473.     close $f
  1474.     set c
  1475. } {hello
  1476. there
  1477. and
  1478. here
  1479. }
  1480. test io-7.17 {Tcl_Write, implicit ^Z at end, Tcl_Read auto} {pcOnly} {
  1481.     removeFile test1
  1482.     set f [open test1 w]
  1483.     fconfigure $f -eofchar \x1a -translation lf
  1484.     puts $f hello\nthere\nand\rhere
  1485.     close $f
  1486.     set f [open test1 r]
  1487.     fconfigure $f -eofchar \x1a -translation auto
  1488.     set c [read $f]
  1489.     close $f
  1490.     set c
  1491. } {hello
  1492. there
  1493. and
  1494. here
  1495. }
  1496. test io-7.18 {Tcl_Write, ^Z in middle, Tcl_Read auto} {
  1497.     removeFile test1
  1498.     set f [open test1 w]
  1499.     fconfigure $f -translation lf
  1500.     set s [format "abc\ndef\n%cghi\nqrs" 26]
  1501.     puts $f $s
  1502.     close $f
  1503.     set f [open test1 r]
  1504.     fconfigure $f -eofchar \x1a -translation auto
  1505.     set l ""
  1506.     lappend l [gets $f]
  1507.     lappend l [gets $f]
  1508.     lappend l [eof $f]
  1509.     lappend l [gets $f]
  1510.     lappend l [eof $f]
  1511.     lappend l [gets $f]
  1512.     lappend l [eof $f]
  1513.     close $f
  1514.     set l
  1515. } {abc def 0 {} 1 {} 1}
  1516. test io-7.19 {Tcl_Write, ^Z no newline in middle, Tcl_Read auto} {
  1517.     removeFile test1
  1518.     set f [open test1 w]
  1519.     fconfigure $f -translation lf
  1520.     set s [format "abc\ndef\n%cghi\nqrs" 26]
  1521.     puts $f $s
  1522.     close $f
  1523.     set f [open test1 r]
  1524.     fconfigure $f -eofchar \x1a -translation auto
  1525.     set l ""
  1526.     lappend l [gets $f]
  1527.     lappend l [gets $f]
  1528.     lappend l [eof $f]
  1529.     lappend l [gets $f]
  1530.     lappend l [eof $f]
  1531.     lappend l [gets $f]
  1532.     lappend l [eof $f]
  1533.     close $f
  1534.     set l
  1535. } {abc def 0 {} 1 {} 1}
  1536. test io-7.20 {Tcl_Write, ^Z in middle ignored, Tcl_Read lf} {
  1537.     removeFile test1
  1538.     set f [open test1 w]
  1539.     fconfigure $f -translation lf -eofchar {}
  1540.     set s [format "abc\ndef\n%cghi\nqrs" 26]
  1541.     puts $f $s
  1542.     close $f
  1543.     set f [open test1 r]
  1544.     fconfigure $f -translation lf -eofchar {}
  1545.     set l ""
  1546.     lappend l [gets $f]
  1547.     lappend l [gets $f]
  1548.     lappend l [eof $f]
  1549.     lappend l [gets $f]
  1550.     lappend l [eof $f]
  1551.     lappend l [gets $f]
  1552.     lappend l [eof $f]
  1553.     lappend l [gets $f]
  1554.     lappend l [eof $f]
  1555.     close $f
  1556.     set l
  1557. } "abc def 0 \x1aghi 0 qrs 0 {} 1"
  1558. test io-7.21 {Tcl_Write, ^Z in middle ignored, Tcl_Read cr} {
  1559.     removeFile test1
  1560.     set f [open test1 w]
  1561.     fconfigure $f -translation lf -eofchar {}
  1562.     set s [format "abc\ndef\n%cghi\nqrs" 26]
  1563.     puts $f $s
  1564.     close $f
  1565.     set f [open test1 r]
  1566.     fconfigure $f -translation cr -eofchar {}
  1567.     set l ""
  1568.     set x [gets $f]
  1569.     lappend l [string compare $x "abc\ndef\n\x1aghi\nqrs"]
  1570.     lappend l [eof $f]
  1571.     lappend l [gets $f]
  1572.     lappend l [eof $f]
  1573.     close $f
  1574.     set l
  1575. } {0 1 {} 1}
  1576. test io-7.22 {Tcl_Write, ^Z in middle ignored, Tcl_Read crlf} {
  1577.     removeFile test1
  1578.     set f [open test1 w]
  1579.     fconfigure $f -translation lf -eofchar {}
  1580.     set s [format "abc\ndef\n%cghi\nqrs" 26]
  1581.     puts $f $s
  1582.     close $f
  1583.     set f [open test1 r]
  1584.     fconfigure $f -translation crlf -eofchar {}
  1585.     set l ""
  1586.     set x [gets $f]
  1587.     lappend l [string compare $x "abc\ndef\n\x1aghi\nqrs"]
  1588.     lappend l [eof $f]
  1589.     lappend l [gets $f]
  1590.     lappend l [eof $f]
  1591.     close $f
  1592.     set l
  1593. } {0 1 {} 1}
  1594. test io-7.23 {Tcl_Write lf, ^Z in middle, Tcl_Read auto} {
  1595.     removeFile test1
  1596.     set f [open test1 w]
  1597.     fconfigure $f -translation lf
  1598.     set c [format abc\ndef\n%cqrs\ntuv 26]
  1599.     puts $f $c
  1600.     close $f
  1601.     set f [open test1 r]
  1602.     fconfigure $f -translation auto -eofchar \x1a
  1603.     set c [string length [read $f]]
  1604.     set e [eof $f]
  1605.     close $f
  1606.     list $c $e
  1607. } {8 1}
  1608. test io-7.24 {Tcl_Write lf, ^Z in middle, Tcl_Read lf} {
  1609.     removeFile test1
  1610.     set f [open test1 w]
  1611.     fconfigure $f -translation lf
  1612.     set c [format abc\ndef\n%cqrs\ntuv 26]
  1613.     puts $f $c
  1614.     close $f
  1615.     set f [open test1 r]
  1616.     fconfigure $f -translation lf -eofchar \x1a
  1617.     set c [string length [read $f]]
  1618.     set e [eof $f]
  1619.     close $f
  1620.     list $c $e
  1621. } {8 1}
  1622. test io-7.25 {Tcl_Write cr, ^Z in middle, Tcl_Read auto} {
  1623.     removeFile test1
  1624.     set f [open test1 w]
  1625.     fconfigure $f -translation cr
  1626.     set c [format abc\ndef\n%cqrs\ntuv 26]
  1627.     puts $f $c
  1628.     close $f
  1629.     set f [open test1 r]
  1630.     fconfigure $f -translation auto -eofchar \x1a
  1631.     set c [string length [read $f]]
  1632.     set e [eof $f]
  1633.     close $f
  1634.     list $c $e
  1635. } {8 1}
  1636. test io-7.26 {Tcl_Write cr, ^Z in middle, Tcl_Read cr} {
  1637.     removeFile test1
  1638.     set f [open test1 w]
  1639.     fconfigure $f -translation cr
  1640.     set c [format abc\ndef\n%cqrs\ntuv 26]
  1641.     puts $f $c
  1642.     close $f
  1643.     set f [open test1 r]
  1644.     fconfigure $f -translation cr -eofchar \x1a
  1645.     set c [string length [read $f]]
  1646.     set e [eof $f]
  1647.     close $f
  1648.     list $c $e
  1649. } {8 1}
  1650. test io-7.27 {Tcl_Write crlf, ^Z in middle, Tcl_Read auto} {
  1651.     removeFile test1
  1652.     set f [open test1 w]
  1653.     fconfigure $f -translation crlf
  1654.     set c [format abc\ndef\n%cqrs\ntuv 26]
  1655.     puts $f $c
  1656.     close $f
  1657.     set f [open test1 r]
  1658.     fconfigure $f -translation auto -eofchar \x1a
  1659.     set c [string length [read $f]]
  1660.     set e [eof $f]
  1661.     close $f
  1662.     list $c $e
  1663. } {8 1}
  1664. test io-7.28 {Tcl_Write crlf, ^Z in middle, Tcl_Read crlf} {
  1665.     removeFile test1
  1666.     set f [open test1 w]
  1667.     fconfigure $f -translation crlf
  1668.     set c [format abc\ndef\n%cqrs\ntuv 26]
  1669.     puts $f $c
  1670.     close $f
  1671.     set f [open test1 r]
  1672.     fconfigure $f -translation crlf -eofchar \x1a
  1673.     set c [string length [read $f]]
  1674.     set e [eof $f]
  1675.     close $f
  1676.     list $c $e
  1677. } {8 1}
  1678.  
  1679. # Test end of line translations. Functions tested are Tcl_Write and Tcl_Gets.
  1680.  
  1681. test io-8.1 {Tcl_Write lf, Tcl_Gets auto} {
  1682.     removeFile test1
  1683.     set f [open test1 w]
  1684.     fconfigure $f -translation lf
  1685.     puts $f hello\nthere\nand\nhere
  1686.     close $f
  1687.     set f [open test1 r]
  1688.     set l ""
  1689.     lappend l [gets $f]
  1690.     lappend l [tell $f]
  1691.     lappend l [fconfigure $f -translation]
  1692.     lappend l [gets $f]
  1693.     lappend l [tell $f]
  1694.     lappend l [fconfigure $f -translation]
  1695.     close $f
  1696.     set l
  1697. } {hello 6 auto there 12 auto}
  1698. test io-8.2 {Tcl_Write cr, Tcl_Gets auto} {
  1699.     removeFile test1
  1700.     set f [open test1 w]
  1701.     fconfigure $f -translation cr
  1702.     puts $f hello\nthere\nand\nhere
  1703.     close $f
  1704.     set f [open test1 r]
  1705.     set l ""
  1706.     lappend l [gets $f]
  1707.     lappend l [tell $f]
  1708.     lappend l [fconfigure $f -translation]
  1709.     lappend l [gets $f]
  1710.     lappend l [tell $f]
  1711.     lappend l [fconfigure $f -translation]
  1712.     close $f
  1713.     set l
  1714. } {hello 6 auto there 12 auto}
  1715. test io-8.3 {Tcl_Write crlf, Tcl_Gets auto} {
  1716.     removeFile test1
  1717.     set f [open test1 w]
  1718.     fconfigure $f -translation crlf
  1719.     puts $f hello\nthere\nand\nhere
  1720.     close $f
  1721.     set f [open test1 r]
  1722.     set l ""
  1723.     lappend l [gets $f]
  1724.     lappend l [tell $f]
  1725.     lappend l [fconfigure $f -translation]
  1726.     lappend l [gets $f]
  1727.     lappend l [tell $f]
  1728.     lappend l [fconfigure $f -translation]
  1729.     close $f
  1730.     set l
  1731. } {hello 7 auto there 14 auto}
  1732. test io-8.4 {Tcl_Write lf, Tcl_Gets lf} {
  1733.     removeFile test1
  1734.     set f [open test1 w]
  1735.     fconfigure $f -translation lf
  1736.     puts $f hello\nthere\nand\nhere
  1737.     close $f
  1738.     set f [open test1 r]
  1739.     fconfigure $f -translation lf
  1740.     set l ""
  1741.     lappend l [gets $f]
  1742.     lappend l [tell $f]
  1743.     lappend l [fconfigure $f -translation]
  1744.     lappend l [gets $f]
  1745.     lappend l [tell $f]
  1746.     lappend l [fconfigure $f -translation]
  1747.     close $f
  1748.     set l
  1749. } {hello 6 lf there 12 lf}
  1750. test io-8.5 {Tcl_Write lf, Tcl_Gets cr} {
  1751.     removeFile test1
  1752.     set f [open test1 w]
  1753.     fconfigure $f -translation lf
  1754.     puts $f hello\nthere\nand\nhere
  1755.     close $f
  1756.     set f [open test1 r]
  1757.     fconfigure $f -translation cr
  1758.     set l ""
  1759.     lappend l [string length [gets $f]]
  1760.     lappend l [tell $f]
  1761.     lappend l [fconfigure $f -translation]
  1762.     lappend l [eof $f]
  1763.     lappend l [gets $f]
  1764.     lappend l [tell $f]
  1765.     lappend l [fconfigure $f -translation]
  1766.     lappend l [eof $f]
  1767.     close $f
  1768.     set l
  1769. } {20 21 cr 1 {} 21 cr 1}
  1770. test io-8.6 {Tcl_Write lf, Tcl_Gets crlf} {
  1771.     removeFile test1
  1772.     set f [open test1 w]
  1773.     fconfigure $f -translation lf
  1774.     puts $f hello\nthere\nand\nhere
  1775.     close $f
  1776.     set f [open test1 r]
  1777.     fconfigure $f -translation crlf
  1778.     set l ""
  1779.     lappend l [string length [gets $f]]
  1780.     lappend l [tell $f]
  1781.     lappend l [fconfigure $f -translation]
  1782.     lappend l [eof $f]
  1783.     lappend l [gets $f]
  1784.     lappend l [tell $f]
  1785.     lappend l [fconfigure $f -translation]
  1786.     lappend l [eof $f]
  1787.     close $f
  1788.     set l
  1789. } {20 21 crlf 1 {} 21 crlf 1}
  1790. test io-8.7 {Tcl_Write cr, Tcl_Gets cr} {
  1791.     removeFile test1
  1792.     set f [open test1 w]
  1793.     fconfigure $f -translation cr
  1794.     puts $f hello\nthere\nand\nhere
  1795.     close $f
  1796.     set f [open test1 r]
  1797.     fconfigure $f -translation cr
  1798.     set l ""
  1799.     lappend l [gets $f]
  1800.     lappend l [tell $f]
  1801.     lappend l [fconfigure $f -translation]
  1802.     lappend l [eof $f]
  1803.     lappend l [gets $f]
  1804.     lappend l [tell $f]
  1805.     lappend l [fconfigure $f -translation]
  1806.     lappend l [eof $f]
  1807.     close $f
  1808.     set l
  1809. } {hello 6 cr 0 there 12 cr 0}
  1810. test io-8.8 {Tcl_Write cr, Tcl_Gets lf} {
  1811.     removeFile test1
  1812.     set f [open test1 w]
  1813.     fconfigure $f -translation cr
  1814.     puts $f hello\nthere\nand\nhere
  1815.     close $f
  1816.     set f [open test1 r]
  1817.     fconfigure $f -translation lf
  1818.     set l ""
  1819.     lappend l [string length [gets $f]]
  1820.     lappend l [tell $f]
  1821.     lappend l [fconfigure $f -translation]
  1822.     lappend l [eof $f]
  1823.     lappend l [gets $f]
  1824.     lappend l [tell $f]
  1825.     lappend l [fconfigure $f -translation]
  1826.     lappend l [eof $f]
  1827.     close $f
  1828.     set l
  1829. } {21 21 lf 1 {} 21 lf 1}
  1830. test io-8.9 {Tcl_Write cr, Tcl_Gets crlf} {
  1831.     removeFile test1
  1832.     set f [open test1 w]
  1833.     fconfigure $f -translation cr
  1834.     puts $f hello\nthere\nand\nhere
  1835.     close $f
  1836.     set f [open test1 r]
  1837.     fconfigure $f -translation crlf
  1838.     set l ""
  1839.     lappend l [string length [gets $f]]
  1840.     lappend l [tell $f]
  1841.     lappend l [fconfigure $f -translation]
  1842.     lappend l [eof $f]
  1843.     lappend l [gets $f]
  1844.     lappend l [tell $f]
  1845.     lappend l [fconfigure $f -translation]
  1846.     lappend l [eof $f]
  1847.     close $f
  1848.     set l
  1849. } {21 21 crlf 1 {} 21 crlf 1}
  1850. test io-8.10 {Tcl_Write crlf, Tcl_Gets crlf} {
  1851.     removeFile test1
  1852.     set f [open test1 w]
  1853.     fconfigure $f -translation crlf
  1854.     puts $f hello\nthere\nand\nhere
  1855.     close $f
  1856.     set f [open test1 r]
  1857.     fconfigure $f -translation crlf
  1858.     set l ""
  1859.     lappend l [gets $f]
  1860.     lappend l [tell $f]
  1861.     lappend l [fconfigure $f -translation]
  1862.     lappend l [eof $f]
  1863.     lappend l [gets $f]
  1864.     lappend l [tell $f]
  1865.     lappend l [fconfigure $f -translation]
  1866.     lappend l [eof $f]
  1867.     close $f
  1868.     set l
  1869. } {hello 7 crlf 0 there 14 crlf 0}
  1870. test io-8.11 {Tcl_Write crlf, Tcl_Gets cr} {
  1871.     removeFile test1
  1872.     set f [open test1 w]
  1873.     fconfigure $f -translation crlf
  1874.     puts $f hello\nthere\nand\nhere
  1875.     close $f
  1876.     set f [open test1 r]
  1877.     fconfigure $f -translation cr
  1878.     set l ""
  1879.     lappend l [gets $f]
  1880.     lappend l [tell $f]
  1881.     lappend l [fconfigure $f -translation]
  1882.     lappend l [eof $f]
  1883.     lappend l [string length [gets $f]]
  1884.     lappend l [tell $f]
  1885.     lappend l [fconfigure $f -translation]
  1886.     lappend l [eof $f]
  1887.     close $f
  1888.     set l
  1889. } {hello 6 cr 0 6 13 cr 0}
  1890. test io-8.12 {Tcl_Write crlf, Tcl_Gets lf} {
  1891.     removeFile test1
  1892.     set f [open test1 w]
  1893.     fconfigure $f -translation crlf
  1894.     puts $f hello\nthere\nand\nhere
  1895.     close $f
  1896.     set f [open test1 r]
  1897.     fconfigure $f -translation lf
  1898.     set l ""
  1899.     lappend l [string length [gets $f]]
  1900.     lappend l [tell $f]
  1901.     lappend l [fconfigure $f -translation]
  1902.     lappend l [eof $f]
  1903.     lappend l [string length [gets $f]]
  1904.     lappend l [tell $f]
  1905.     lappend l [fconfigure $f -translation]
  1906.     lappend l [eof $f]
  1907.     close $f
  1908.     set l
  1909. } {6 7 lf 0 6 14 lf 0}
  1910. test io-8.13 {binary mode is synonym of lf mode} {
  1911.     removeFile test1
  1912.     set f [open test1 w]
  1913.     fconfigure $f -translation binary
  1914.     set x [fconfigure $f -translation]
  1915.     close $f
  1916.     set x
  1917. } lf
  1918. #
  1919. # Test io-9.14 has been removed because "auto" output translation mode is
  1920. # not supoprted.
  1921. #
  1922. test io-8.14 {Tcl_Write mixed, Tcl_Gets auto} {
  1923.     removeFile test1
  1924.     set f [open test1 w]
  1925.     fconfigure $f -translation lf
  1926.     puts $f hello\nthere\rand\r\nhere
  1927.     close $f
  1928.     set f [open test1 r]
  1929.     fconfigure $f -translation auto
  1930.     set l ""
  1931.     lappend l [gets $f]
  1932.     lappend l [gets $f]
  1933.     lappend l [gets $f]
  1934.     lappend l [gets $f]
  1935.     lappend l [eof $f]
  1936.     lappend l [gets $f]
  1937.     lappend l [eof $f]
  1938.     close $f
  1939.     set l
  1940. } {hello there and here 0 {} 1}
  1941. test io-8.15 {Tcl_Write mixed, Tcl_Gets auto} {
  1942.     removeFile test1
  1943.     set f [open test1 w]
  1944.     fconfigure $f -translation lf
  1945.     puts -nonewline $f hello\nthere\rand\r\nhere\r
  1946.     close $f
  1947.     set f [open test1 r]
  1948.     fconfigure $f -translation auto
  1949.     set l ""
  1950.     lappend l [gets $f]
  1951.     lappend l [gets $f]
  1952.     lappend l [gets $f]
  1953.     lappend l [gets $f]
  1954.     lappend l [eof $f]
  1955.     lappend l [gets $f]
  1956.     lappend l [eof $f]
  1957.     close $f
  1958.     set l
  1959. } {hello there and here 0 {} 1}
  1960. test io-8.16 {Tcl_Write mixed, Tcl_Gets auto} {
  1961.     removeFile test1
  1962.     set f [open test1 w]
  1963.     fconfigure $f -translation lf
  1964.     puts -nonewline $f hello\nthere\rand\r\nhere\n
  1965.     close $f
  1966.     set f [open test1 r]
  1967.     set l ""
  1968.     lappend l [gets $f]
  1969.     lappend l [gets $f]
  1970.     lappend l [gets $f]
  1971.     lappend l [gets $f]
  1972.     lappend l [eof $f]
  1973.     lappend l [gets $f]
  1974.     lappend l [eof $f]
  1975.     close $f
  1976.     set l
  1977. } {hello there and here 0 {} 1}
  1978. test io-8.17 {Tcl_Write mixed, Tcl_Gets auto} {
  1979.     removeFile test1
  1980.     set f [open test1 w]
  1981.     fconfigure $f -translation lf
  1982.     puts -nonewline $f hello\nthere\rand\r\nhere\r\n
  1983.     close $f
  1984.     set f [open test1 r]
  1985.     fconfigure $f -translation auto
  1986.     set l ""
  1987.     lappend l [gets $f]
  1988.     lappend l [gets $f]
  1989.     lappend l [gets $f]
  1990.     lappend l [gets $f]
  1991.     lappend l [eof $f]
  1992.     lappend l [gets $f]
  1993.     lappend l [eof $f]
  1994.     close $f
  1995.     set l
  1996. } {hello there and here 0 {} 1}
  1997. test io-8.18 {Tcl_Write ^Z at end, Tcl_Gets auto} {
  1998.     removeFile test1
  1999.     set f [open test1 w]
  2000.     fconfigure $f -translation lf
  2001.     set s [format "hello\nthere\nand\rhere\n\%c" 26]
  2002.     puts $f $s
  2003.     close $f
  2004.     set f [open test1 r]
  2005.     fconfigure $f -eofchar \x1a -translation auto
  2006.     set l ""
  2007.     lappend l [gets $f]
  2008.     lappend l [gets $f]
  2009.     lappend l [gets $f]
  2010.     lappend l [gets $f]
  2011.     lappend l [eof $f]
  2012.     lappend l [gets $f]
  2013.     lappend l [eof $f]
  2014.     close $f
  2015.     set l
  2016. } {hello there and here 0 {} 1}
  2017. test io-8.19 {Tcl_Write, implicit ^Z at end, Tcl_Gets auto} {
  2018.     removeFile test1
  2019.     set f [open test1 w]
  2020.     fconfigure $f -eofchar \x1a -translation lf
  2021.     puts $f hello\nthere\nand\rhere
  2022.     close $f
  2023.     set f [open test1 r]
  2024.     fconfigure $f -eofchar \x1a -translation auto
  2025.     set l ""
  2026.     lappend l [gets $f]
  2027.     lappend l [gets $f]
  2028.     lappend l [gets $f]
  2029.     lappend l [gets $f]
  2030.     lappend l [eof $f]
  2031.     lappend l [gets $f]
  2032.     lappend l [eof $f]
  2033.     close $f
  2034.     set l
  2035. } {hello there and here 0 {} 1}
  2036. test io-8.20 {Tcl_Write, ^Z in middle, Tcl_Gets auto, eofChar} {
  2037.     removeFile test1
  2038.     set f [open test1 w]
  2039.     fconfigure $f -translation lf
  2040.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2041.     puts $f $s
  2042.     close $f
  2043.     set f [open test1 r]
  2044.     fconfigure $f -eofchar \x1a
  2045.     fconfigure $f -translation auto
  2046.     set l ""
  2047.     lappend l [gets $f]
  2048.     lappend l [gets $f]
  2049.     lappend l [eof $f]
  2050.     lappend l [gets $f]
  2051.     lappend l [eof $f]
  2052.     close $f
  2053.     set l
  2054. } {abc def 0 {} 1}
  2055. test io-8.21 {Tcl_Write, no newline ^Z in middle, Tcl_Gets auto, eofChar} {
  2056.     removeFile test1
  2057.     set f [open test1 w]
  2058.     fconfigure $f -translation lf
  2059.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2060.     puts $f $s
  2061.     close $f
  2062.     set f [open test1 r]
  2063.     fconfigure $f -eofchar \x1a -translation auto
  2064.     set l ""
  2065.     lappend l [gets $f]
  2066.     lappend l [gets $f]
  2067.     lappend l [eof $f]
  2068.     lappend l [gets $f]
  2069.     lappend l [eof $f]
  2070.     close $f
  2071.     set l
  2072. } {abc def 0 {} 1}
  2073. test io-8.22 {Tcl_Write, ^Z in middle ignored, Tcl_Gets lf} {
  2074.     removeFile test1
  2075.     set f [open test1 w]
  2076.     fconfigure $f -translation lf -eofchar {}
  2077.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2078.     puts $f $s
  2079.     close $f
  2080.     set f [open test1 r]
  2081.     fconfigure $f -translation lf -eofchar {}
  2082.     set l ""
  2083.     lappend l [gets $f]
  2084.     lappend l [gets $f]
  2085.     lappend l [eof $f]
  2086.     lappend l [gets $f]
  2087.     lappend l [eof $f]
  2088.     lappend l [gets $f]
  2089.     lappend l [eof $f]
  2090.     lappend l [gets $f]
  2091.     lappend l [eof $f]
  2092.     close $f
  2093.     set l
  2094. } "abc def 0 \x1aqrs 0 tuv 0 {} 1"
  2095. test io-8.23 {Tcl_Write, ^Z in middle ignored, Tcl_Gets cr} {
  2096.     removeFile test1
  2097.     set f [open test1 w]
  2098.     fconfigure $f -translation cr -eofchar {}
  2099.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2100.     puts $f $s
  2101.     close $f
  2102.     set f [open test1 r]
  2103.     fconfigure $f -translation cr -eofchar {}
  2104.     set l ""
  2105.     lappend l [gets $f]
  2106.     lappend l [gets $f]
  2107.     lappend l [eof $f]
  2108.     lappend l [gets $f]
  2109.     lappend l [eof $f]
  2110.     lappend l [gets $f]
  2111.     lappend l [eof $f]
  2112.     lappend l [gets $f]
  2113.     lappend l [eof $f]
  2114.     close $f
  2115.     set l
  2116. } "abc def 0 \x1aqrs 0 tuv 0 {} 1"
  2117. test io-8.24 {Tcl_Write, ^Z in middle ignored, Tcl_Gets crlf} {
  2118.     removeFile test1
  2119.     set f [open test1 w]
  2120.     fconfigure $f -translation crlf -eofchar {}
  2121.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2122.     puts $f $s
  2123.     close $f
  2124.     set f [open test1 r]
  2125.     fconfigure $f -translation crlf -eofchar {}
  2126.     set l ""
  2127.     lappend l [gets $f]
  2128.     lappend l [gets $f]
  2129.     lappend l [eof $f]
  2130.     lappend l [gets $f]
  2131.     lappend l [eof $f]
  2132.     lappend l [gets $f]
  2133.     lappend l [eof $f]
  2134.     lappend l [gets $f]
  2135.     lappend l [eof $f]
  2136.     close $f
  2137.     set l
  2138. } "abc def 0 \x1aqrs 0 tuv 0 {} 1"
  2139. test io-8.25 {Tcl_Write lf, ^Z in middle, Tcl_Gets auto} {
  2140.     removeFile test1
  2141.     set f [open test1 w]
  2142.     fconfigure $f -translation lf
  2143.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2144.     puts $f $s
  2145.     close $f
  2146.     set f [open test1 r]
  2147.     fconfigure $f -translation auto -eofchar \x1a
  2148.     set l ""
  2149.     lappend l [gets $f]
  2150.     lappend l [gets $f]
  2151.     lappend l [eof $f]
  2152.     lappend l [gets $f]
  2153.     lappend l [eof $f]
  2154.     close $f
  2155.     set l
  2156. } {abc def 0 {} 1}
  2157. test io-8.26 {Tcl_Write lf, ^Z in middle, Tcl_Gets lf} {
  2158.     removeFile test1
  2159.     set f [open test1 w]
  2160.     fconfigure $f -translation lf
  2161.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2162.     puts $f $s
  2163.     close $f
  2164.     set f [open test1 r]
  2165.     fconfigure $f -translation lf -eofchar \x1a
  2166.     set l ""
  2167.     lappend l [gets $f]
  2168.     lappend l [gets $f]
  2169.     lappend l [eof $f]
  2170.     lappend l [gets $f]
  2171.     lappend l [eof $f]
  2172.     close $f
  2173.     set l
  2174. } {abc def 0 {} 1}
  2175. test io-8.27 {Tcl_Write cr, ^Z in middle, Tcl_Gets auto} {
  2176.     removeFile test1
  2177.     set f [open test1 w]
  2178.     fconfigure $f -translation cr -eofchar {}
  2179.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2180.     puts $f $s
  2181.     close $f
  2182.     set f [open test1 r]
  2183.     fconfigure $f -translation auto -eofchar \x1a
  2184.     set l ""
  2185.     lappend l [gets $f]
  2186.     lappend l [gets $f]
  2187.     lappend l [eof $f]
  2188.     lappend l [gets $f]
  2189.     lappend l [eof $f]
  2190.     close $f
  2191.     set l
  2192. } {abc def 0 {} 1}
  2193. test io-8.28 {Tcl_Write cr, ^Z in middle, Tcl_Gets cr} {
  2194.     removeFile test1
  2195.     set f [open test1 w]
  2196.     fconfigure $f -translation cr -eofchar {}
  2197.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2198.     puts $f $s
  2199.     close $f
  2200.     set f [open test1 r]
  2201.     fconfigure $f -translation cr -eofchar \x1a
  2202.     set l ""
  2203.     lappend l [gets $f]
  2204.     lappend l [gets $f]
  2205.     lappend l [eof $f]
  2206.     lappend l [gets $f]
  2207.     lappend l [eof $f]
  2208.     close $f
  2209.     set l
  2210. } {abc def 0 {} 1}
  2211. test io-8.29 {Tcl_Write crlf, ^Z in middle, Tcl_Gets auto} {
  2212.     removeFile test1
  2213.     set f [open test1 w]
  2214.     fconfigure $f -translation crlf -eofchar {}
  2215.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2216.     puts $f $s
  2217.     close $f
  2218.     set f [open test1 r]
  2219.     fconfigure $f -translation auto -eofchar \x1a
  2220.     set l ""
  2221.     lappend l [gets $f]
  2222.     lappend l [gets $f]
  2223.     lappend l [eof $f]
  2224.     lappend l [gets $f]
  2225.     lappend l [eof $f]
  2226.     close $f
  2227.     set l
  2228. } {abc def 0 {} 1}
  2229. test io-8.30 {Tcl_Write crlf, ^Z in middle, Tcl_Gets crlf} {
  2230.     removeFile test1
  2231.     set f [open test1 w]
  2232.     fconfigure $f -translation crlf -eofchar {}
  2233.     set s [format "abc\ndef\n%cqrs\ntuv" 26]
  2234.     puts $f $s
  2235.     close $f
  2236.     set f [open test1 r]
  2237.     fconfigure $f -translation crlf -eofchar \x1a
  2238.     set l ""
  2239.     lappend l [gets $f]
  2240.     lappend l [gets $f]
  2241.     lappend l [eof $f]
  2242.     lappend l [gets $f]
  2243.     lappend l [eof $f]
  2244.     close $f
  2245.     set l
  2246. } {abc def 0 {} 1}
  2247. test io-8.31 {Tcl_Write crlf on block boundary, Tcl_Gets auto} {
  2248.     removeFile test1
  2249.     set f [open test1 w]
  2250.     fconfigure $f -translation crlf
  2251.     set line "123456789ABCDE"    ;# 14 char plus crlf
  2252.     puts -nonewline $f x    ;# shift crlf across block boundary
  2253.     for {set i 0} {$i < 700} {incr i} {
  2254.     puts $f $line
  2255.     }
  2256.     close $f
  2257.     set f [open test1 r]
  2258.     fconfigure $f -translation auto
  2259.     set c ""
  2260.     while {[gets $f line] >= 0} {
  2261.     append c $line\n
  2262.     }
  2263.     close $f
  2264.     string length $c
  2265. } [expr 700*15+1]
  2266. test io-8.32 {Tcl_Write crlf on block boundary, Tcl_Gets auto} {
  2267.     removeFile test1
  2268.     set f [open test1 w]
  2269.     fconfigure $f -translation crlf
  2270.     set line "123456789ABCDE"    ;# 14 char plus crlf
  2271.     puts -nonewline $f x    ;# shift crlf across block boundary
  2272.     for {set i 0} {$i < 256} {incr i} {
  2273.     puts $f $line
  2274.     }
  2275.     close $f
  2276.     set f [open test1 r]
  2277.     fconfigure $f -translation auto
  2278.     set c ""
  2279.     while {[gets $f line] >= 0} {
  2280.     append c $line\n
  2281.     }
  2282.     close $f
  2283.     string length $c
  2284. } [expr 256*15+1]
  2285.  
  2286.  
  2287. # Test Tcl_Read and buffering.
  2288.  
  2289. test io-9.1 {Tcl_Read, channel not readable} {
  2290.     list [catch {read stdout} msg] $msg
  2291. } {1 {channel "stdout" wasn't opened for reading}}
  2292. test io-9.2 {Tcl_Read, zero byte count} {
  2293.     read stdin 0
  2294. } ""
  2295. test io-9.3 {Tcl_Read, negative byte count} {
  2296.     set f [open longfile r]
  2297.     set l [list [catch {read $f -1} msg] $msg]
  2298.     close $f
  2299.     set l
  2300. } {1 {bad argument "-1": should be "nonewline"}}
  2301. test io-9.4 {Tcl_Read, positive byte count} {
  2302.     set f [open longfile r]
  2303.     set x [read $f 1024]
  2304.     set s [string length $x]
  2305.     unset x
  2306.     close $f
  2307.     set s
  2308. } 1024
  2309. test io-9.5 {Tcl_Read, multiple buffers} {
  2310.     set f [open longfile r]
  2311.     fconfigure $f -buffersize 100
  2312.     set x [read $f 1024]
  2313.     set s [string length $x]
  2314.     unset x
  2315.     close $f
  2316.     set s
  2317. } 1024
  2318. test io-9.6 {Tcl_Read, very large read} {
  2319.     set f1 [open longfile r]
  2320.     set z [read $f1 1000000]
  2321.     close $f1
  2322.     set l [string length $z]
  2323.     set x ok
  2324.     set z [file size longfile]
  2325.     if {$z != $l} {
  2326.         set x broken
  2327.     }
  2328.     set x
  2329. } ok
  2330. test io-9.7 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
  2331.     set f1 [open longfile r]
  2332.     fconfigure $f1 -blocking off
  2333.     set z [read $f1 20]
  2334.     close $f1
  2335.     set l [string length $z]
  2336.     set x ok
  2337.     if {$l != 20} {
  2338.         set x broken
  2339.     }
  2340.     set x
  2341. } ok
  2342. test io-9.8 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
  2343.     set f1 [open longfile r]
  2344.     fconfigure $f1 -blocking off
  2345.     set z [read $f1 1000000]
  2346.     close $f1
  2347.     set x ok
  2348.     set l [string length $z]]
  2349.     set z [file size longfile]]
  2350.     if {$z != $l} {
  2351.         set x broken
  2352.     }
  2353.   set x
  2354. } ok
  2355. test io-9.9 {Tcl_Read, read to end of file} {
  2356.     set f1 [open longfile r]
  2357.     set z [read $f1]
  2358.     close $f1
  2359.     set l [string length $z]
  2360.     set x ok
  2361.     set z [file size longfile]
  2362.     if {$z != $l} {
  2363.         set x broken
  2364.     }
  2365.     set x
  2366. } ok
  2367. test io-9.10 {Tcl_Read from a pipe} {stdio} {
  2368.     removeFile pipe
  2369.     set f1 [open pipe w]
  2370.     puts $f1 {puts [gets stdin]}
  2371.     close $f1
  2372.     set f1 [open "|[list $tcltest pipe]" r+]
  2373.     puts $f1 hello
  2374.     flush $f1
  2375.     set x [read $f1]
  2376.     close $f1
  2377.     set x
  2378. } "hello\n"
  2379. test io-9.11 {Tcl_Read from a pipe} {stdio} {
  2380.     removeFile pipe
  2381.     set f1 [open pipe w]
  2382.     puts $f1 {puts [gets stdin]}
  2383.     puts $f1 {puts [gets stdin]}
  2384.     close $f1
  2385.     set f1 [open "|[list $tcltest pipe]" r+]
  2386.     puts $f1 hello
  2387.     flush $f1
  2388.     set x ""
  2389.     lappend x [read $f1 6]
  2390.     puts $f1 hello
  2391.     flush $f1
  2392.     lappend x [read $f1]
  2393.     close $f1
  2394.     set x
  2395. } {{hello
  2396. } {hello
  2397. }}
  2398. test io-9.12 {Tcl_Read, -nonewline} {
  2399.     removeFile test1
  2400.     set f1 [open test1 w]
  2401.     puts $f1 hello
  2402.     puts $f1 bye
  2403.     close $f1
  2404.     set f1 [open test1 r]
  2405.     set c [read -nonewline $f1]
  2406.     close $f1
  2407.     set c
  2408. } {hello
  2409. bye}
  2410. test io-9.13 {Tcl_Read, -nonewline} {
  2411.     removeFile test1
  2412.     set f1 [open test1 w]
  2413.     puts $f1 hello
  2414.     puts $f1 bye
  2415.     close $f1
  2416.     set f1 [open test1 r]
  2417.     set c [read -nonewline $f1]
  2418.     close $f1
  2419.     list [string length $c] $c
  2420. } {9 {hello
  2421. bye}}
  2422. test io-9.14 {Tcl_Read, reading in small chunks} {
  2423.     removeFile test1
  2424.     set f [open test1 w]
  2425.     puts $f "Two lines: this one"
  2426.     puts $f "and this one"
  2427.     close $f
  2428.     set f [open test1]
  2429.     set x [list [read $f 1] [read $f 2] [read $f]]
  2430.     close $f
  2431.     set x
  2432. } {T wo { lines: this one
  2433. and this one
  2434. }}
  2435. test io-9.15 {Tcl_Read, asking for more input than available} {
  2436.     removeFile test1
  2437.     set f [open test1 w]
  2438.     puts $f "Two lines: this one"
  2439.     puts $f "and this one"
  2440.     close $f
  2441.     set f [open test1]
  2442.     set x [read $f 100]
  2443.     close $f
  2444.     set x
  2445. } {Two lines: this one
  2446. and this one
  2447. }
  2448. test io-9.16 {Tcl_Read, read to end of file with -nonewline} {
  2449.     removeFile test1
  2450.     set f [open test1 w]
  2451.     puts $f "Two lines: this one"
  2452.     puts $f "and this one"
  2453.     close $f
  2454.     set f [open test1]
  2455.     set x [read -nonewline $f]
  2456.     close $f
  2457.     set x
  2458. } {Two lines: this one
  2459. and this one}
  2460.  
  2461. # Test Tcl_Gets.
  2462.  
  2463. test io-10.1 {Tcl_Gets, reading what was written} {
  2464.     removeFile test1
  2465.     set f1 [open test1 w]
  2466.     set y "first line"
  2467.     puts $f1 $y
  2468.     close $f1
  2469.     set f1 [open test1 r]
  2470.     set x [gets $f1]
  2471.     set z ok
  2472.     if {"$x" != "$y"} {
  2473.         set z broken
  2474.     }
  2475.     close $f1
  2476.     set z
  2477. } ok
  2478. test io-10.2 {Tcl_Gets into variable} {
  2479.     set f1 [open longfile r]
  2480.     set c [gets $f1 x]
  2481.     set l [string length x]
  2482.     set z ok
  2483.     if {$l != $l} {
  2484.         set z broken
  2485.     }
  2486.     close $f1
  2487.     set z
  2488. } ok
  2489. test io-10.3 {Tcl_Gets from pipe} {stdio} {
  2490.     removeFile pipe
  2491.     set f1 [open pipe w]
  2492.     puts $f1 {puts [gets stdin]}
  2493.     close $f1
  2494.     set f1 [open "|[list $tcltest pipe]" r+]
  2495.     puts $f1 hello
  2496.     flush $f1
  2497.     set x [gets $f1]
  2498.     close $f1
  2499.     set z ok
  2500.     if {"$x" != "hello"} {
  2501.         set z broken
  2502.     }
  2503.     set z
  2504. } ok
  2505. test io-10.4 {Tcl_Gets with long line} {
  2506.     removeFile test3
  2507.     set f [open test3 w]
  2508.     puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2509.     puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2510.     puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2511.     puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2512.     puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2513.     close $f
  2514.     set f [open test3]
  2515.     set x [gets $f]
  2516.     close $f
  2517.     set x
  2518. } {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
  2519. test io-10.5 {Tcl_Gets with long line} {
  2520.     set f [open test3]
  2521.     set x [gets $f y]
  2522.     close $f
  2523.     list $x $y
  2524. } {260 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
  2525. test io-10.6 {Tcl_Gets and end of file} {
  2526.     removeFile test3
  2527.     set f [open test3 w]
  2528.     puts -nonewline $f "Test1\nTest2"
  2529.     close $f
  2530.     set f [open test3]
  2531.     set x {}
  2532.     set y {}
  2533.     lappend x [gets $f y] $y
  2534.     set y {}
  2535.     lappend x [gets $f y] $y
  2536.     set y {}
  2537.     lappend x [gets $f y] $y
  2538.     close $f
  2539.     set x
  2540. } {5 Test1 5 Test2 -1 {}}
  2541. test io-10.7 {Tcl_Gets and bad variable} {
  2542.     set f [open test3 w]
  2543.     puts $f "Line 1"
  2544.     puts $f "Line 2"
  2545.     close $f
  2546.     catch {unset x}
  2547.     set x 24
  2548.     set f [open test3 r]
  2549.     set result [list [catch {gets $f x(0)} msg] $msg]
  2550.     close $f
  2551.     set result
  2552. } {1 {can't set "x(0)": variable isn't array}}
  2553. test io-10.8 {Tcl_Gets, exercising double buffering} {
  2554.     set f [open test3 w]
  2555.     fconfigure $f -translation lf -eofchar {}
  2556.     set x ""
  2557.     for {set y 0} {$y < 99} {incr y} {set x "a$x"}
  2558.     for {set y 0} {$y < 100} {incr y} {puts $f $x}
  2559.     close $f
  2560.     set f [open test3 r]
  2561.     fconfigure $f -translation lf
  2562.     for {set y 0} {$y < 100} {incr y} {gets $f}
  2563.     close $f
  2564.     set y
  2565. } 100
  2566. test io-10.9 {Tcl_Gets, exercising double buffering} {
  2567.     set f [open test3 w]
  2568.     fconfigure $f -translation lf -eofchar {}
  2569.     set x ""
  2570.     for {set y 0} {$y < 99} {incr y} {set x "a$x"}
  2571.     for {set y 0} {$y < 200} {incr y} {puts $f $x}
  2572.     close $f
  2573.     set f [open test3 r]
  2574.     fconfigure $f -translation lf
  2575.     for {set y 0} {$y < 200} {incr y} {gets $f}
  2576.     close $f
  2577.     set y
  2578. } 200
  2579. test io-10.10 {Tcl_Gets, exercising double buffering} {
  2580.     set f [open test3 w]
  2581.     fconfigure $f -translation lf -eofchar {}
  2582.     set x ""
  2583.     for {set y 0} {$y < 99} {incr y} {set x "a$x"}
  2584.     for {set y 0} {$y < 300} {incr y} {puts $f $x}
  2585.     close $f
  2586.     set f [open test3 r]
  2587.     fconfigure $f -translation lf
  2588.     for {set y 0} {$y < 300} {incr y} {gets $f}
  2589.     close $f
  2590.     set y
  2591. } 300
  2592.  
  2593. # Test Tcl_Seek and Tcl_Tell.
  2594.  
  2595. test io-11.1 {Tcl_Seek to current position at start of file} {
  2596.     set f1 [open longfile r]
  2597.     seek $f1 0 current
  2598.     set c [tell $f1]
  2599.     close $f1
  2600.     set c
  2601. } 0
  2602. test io-11.2 {Tcl_Seek to offset from start} {
  2603.     removeFile test1
  2604.     set f1 [open test1 w]
  2605.     fconfigure $f1 -translation lf -eofchar {}
  2606.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2607.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2608.     close $f1
  2609.     set f1 [open test1 r]
  2610.     seek $f1 10 start
  2611.     set c [tell $f1]
  2612.     close $f1
  2613.     set c
  2614. } 10
  2615. test io-11.3 {Tcl_Seek to end of file} {
  2616.     removeFile test1
  2617.     set f1 [open test1 w]
  2618.     fconfigure $f1 -translation lf -eofchar {}
  2619.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2620.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2621.     close $f1
  2622.     set f1 [open test1 r]
  2623.     seek $f1 0 end
  2624.     set c [tell $f1]
  2625.     close $f1
  2626.     set c
  2627. } 54
  2628. test io-11.4 {Tcl_Seek to offset from end of file} {
  2629.     removeFile test1
  2630.     set f1 [open test1 w]
  2631.     fconfigure $f1 -translation lf -eofchar {}
  2632.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2633.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2634.     close $f1
  2635.     set f1 [open test1 r]
  2636.     seek $f1 -10 end
  2637.     set c [tell $f1]
  2638.     close $f1
  2639.     set c
  2640. } 44
  2641. test io-11.5 {Tcl_Seek to offset from current position} {
  2642.     removeFile test1
  2643.     set f1 [open test1 w]
  2644.     fconfigure $f1 -translation lf -eofchar {}
  2645.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2646.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2647.     close $f1
  2648.     set f1 [open test1 r]
  2649.     seek $f1 10 current
  2650.     seek $f1 10 current
  2651.     set c [tell $f1]
  2652.     close $f1
  2653.     set c
  2654. } 20
  2655. test io-11.6 {Tcl_Seek to offset from end of file} {
  2656.     removeFile test1
  2657.     set f1 [open test1 w]
  2658.     fconfigure $f1 -translation lf -eofchar {}
  2659.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2660.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2661.     close $f1
  2662.     set f1 [open test1 r]
  2663.     seek $f1 -10 end
  2664.     set c [tell $f1]
  2665.     set r [read $f1]
  2666.     close $f1
  2667.     list $c $r
  2668. } {44 {rstuvwxyz
  2669. }}
  2670. test io-11.7 {Tcl_Seek to offset from end of file, then to current position} {
  2671.     removeFile test1
  2672.     set f1 [open test1 w]
  2673.     fconfigure $f1 -translation lf -eofchar {}
  2674.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2675.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2676.     close $f1
  2677.     set f1 [open test1 r]
  2678.     seek $f1 -10 end
  2679.     set c1 [tell $f1]
  2680.     set r1 [read $f1 5]
  2681.     seek $f1 0 current
  2682.     set c2 [tell $f1]
  2683.     close $f1
  2684.     list $c1 $r1 $c2
  2685. } {44 rstuv 49}
  2686. test io-11.8 {Tcl_Seek on pipes: not supported} {stdio} {
  2687.     set f1 [open "|[list $tcltest]" r+]
  2688.     set x [list [catch {seek $f1 0 current} msg] $msg]
  2689.     close $f1
  2690.     regsub {".*":} $x {"":} x
  2691.     string tolower $x
  2692. } {1 {error during seek on "": invalid argument}}
  2693. test io-11.9 {Tcl_Seek, testing buffered input flushing} {
  2694.     removeFile test3
  2695.     set f [open test3 w]
  2696.     fconfigure $f -eofchar {}
  2697.     puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2698.     close $f
  2699.     set f [open test3 RDWR]
  2700.     set x [read $f 1]
  2701.     seek $f 3
  2702.     lappend x [read $f 1]
  2703.     seek $f 0 start
  2704.     lappend x [read $f 1]
  2705.     seek $f 10 current
  2706.     lappend x [read $f 1]
  2707.     seek $f -2 end
  2708.     lappend x [read $f 1]
  2709.     seek $f 50 end
  2710.     lappend x [read $f 1]
  2711.     seek $f 1
  2712.     lappend x [read $f 1]
  2713.     close $f
  2714.     set x
  2715. } {a d a l Y {} b}
  2716. test io-11.10 {Tcl_Seek testing flushing of buffered input} {
  2717.     set f [open test3 w]
  2718.     fconfigure $f -translation lf
  2719.     puts $f xyz\n123
  2720.     close $f
  2721.     set f [open test3 r+]
  2722.     fconfigure $f -translation lf
  2723.     set x [gets $f]
  2724.     seek $f 0 current
  2725.     puts $f 456
  2726.     close $f
  2727.     list $x [viewFile test3]
  2728. } "xyz {xyz
  2729. 456}"
  2730. test io-11.11 {Tcl_Seek testing flushing of buffered output} {
  2731.     set f [open test3 w]
  2732.     puts $f xyz\n123
  2733.     close $f
  2734.     set f [open test3 w+]
  2735.     puts $f xyzzy
  2736.     seek $f 2
  2737.     set x [gets $f]
  2738.     close $f
  2739.     list $x [viewFile test3]
  2740. } "zzy xyzzy"
  2741. test io-11.12 {Tcl_Seek testing combination of write, seek back and read} {
  2742.     set f [open test3 w]
  2743.     fconfigure $f -translation lf -eofchar {}
  2744.     puts $f xyz\n123
  2745.     close $f
  2746.     set f [open test3 a+]
  2747.     fconfigure $f -translation lf -eofchar {}
  2748.     puts $f xyzzy
  2749.     flush $f
  2750.     set x [tell $f]
  2751.     seek $f -4 cur
  2752.     set y [gets $f]
  2753.     close $f
  2754.     list $x [viewFile test3] $y
  2755. } {14 {xyz
  2756. 123
  2757. xyzzy} zzy}
  2758. test io-11.13 {Tcl_Tell at start of file} {
  2759.     removeFile test1
  2760.     set f1 [open test1 w]
  2761.     set p [tell $f1]
  2762.     close $f1
  2763.     set p
  2764. } 0
  2765. test io-11.14 {Tcl_Tell after seek to end of file} {
  2766.     removeFile test1
  2767.     set f1 [open test1 w]
  2768.     fconfigure $f1 -translation lf -eofchar {}
  2769.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2770.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2771.     close $f1
  2772.     set f1 [open test1 r]
  2773.     seek $f1 0 end
  2774.     set c1 [tell $f1]
  2775.     close $f1
  2776.     set c1
  2777. } 54
  2778. test io-11.15 {Tcl_Tell combined with seeking} {
  2779.     removeFile test1
  2780.     set f1 [open test1 w]
  2781.     fconfigure $f1 -translation lf -eofchar {}
  2782.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2783.     puts $f1 "abcdefghijklmnopqrstuvwxyz"
  2784.     close $f1
  2785.     set f1 [open test1 r]
  2786.     seek $f1 10 start
  2787.     set c1 [tell $f1]
  2788.     seek $f1 10 current
  2789.     set c2 [tell $f1]
  2790.     close $f1
  2791.     list $c1 $c2
  2792. } {10 20}
  2793. test io-11.16 {Tcl_tell on pipe: always -1} {stdio} {
  2794.     set f1 [open "|[list $tcltest]" r+]
  2795.     set c [tell $f1]
  2796.     close $f1
  2797.     set c
  2798. } -1
  2799. test io-11.17 {Tcl_Tell on pipe: always -1} {stdio} {
  2800.     set f1 [open "|[list $tcltest]" r+]
  2801.     puts $f1 {puts hello}
  2802.     flush $f1
  2803.     set c [tell $f1]
  2804.     gets $f1
  2805.     close $f1
  2806.     set c
  2807. } -1
  2808. test io-11.18 {Tcl_Tell combined with seeking and reading} {
  2809.     removeFile test2
  2810.     set f [open test2 w]
  2811.     fconfigure $f -translation lf -eofchar {}
  2812.     puts -nonewline $f "line1\nline2\nline3\nline4\nline5\n"
  2813.     close $f
  2814.     set f [open test2]
  2815.     fconfigure $f -translation lf
  2816.     set x [tell $f]
  2817.     read $f 3
  2818.     lappend x [tell $f]
  2819.     seek $f 2
  2820.     lappend x [tell $f]
  2821.     seek $f 10 current
  2822.     lappend x [tell $f]
  2823.     seek $f 0 end
  2824.     lappend x [tell $f]
  2825.     close $f
  2826.     set x
  2827. } {0 3 2 12 30}
  2828. test io-11.19 {Tcl_Tell combined with opening in append mode} {
  2829.     set f [open test3 w]
  2830.     fconfigure $f -translation lf -eofchar {}
  2831.     puts $f "abcdefghijklmnopqrstuvwxyz"
  2832.     puts $f "abcdefghijklmnopqrstuvwxyz"
  2833.     close $f
  2834.     set f [open test3 a]
  2835.     set c [tell $f]
  2836.     close $f
  2837.     set c
  2838. } 54
  2839. test io-11.20 {Tcl_Tell combined with writing} {
  2840.     set f [open test3 w]
  2841.     set l ""
  2842.     seek $f 29 start
  2843.     lappend l [tell $f]
  2844.     puts -nonewline $f a
  2845.     seek $f 39 start
  2846.     lappend l [tell $f]
  2847.     puts -nonewline $f a
  2848.     lappend l [tell $f]
  2849.     seek $f 407 end
  2850.     lappend l [tell $f]
  2851.     close $f
  2852.     set l
  2853. } {29 39 40 447}
  2854.  
  2855. # Test Tcl_Eof
  2856.  
  2857. test io-12.1 {Tcl_Eof} {
  2858.     removeFile test1
  2859.     set f [open test1 w]
  2860.     puts $f hello
  2861.     puts $f hello
  2862.     close $f
  2863.     set f [open test1]
  2864.     set x [eof $f]
  2865.     lappend x [eof $f]
  2866.     gets $f
  2867.     lappend x [eof $f]
  2868.     gets $f
  2869.     lappend x [eof $f]
  2870.     gets $f
  2871.     lappend x [eof $f]
  2872.     lappend x [eof $f]
  2873.     close $f
  2874.     set x
  2875. } {0 0 0 0 1 1}
  2876. test io-12.2 {Tcl_Eof with pipe} {stdio} {
  2877.     removeFile pipe
  2878.     set f1 [open pipe w]
  2879.     puts $f1 {gets stdin}
  2880.     puts $f1 {puts hello}
  2881.     close $f1
  2882.     set f1 [open "|[list $tcltest pipe]" r+]
  2883.     puts $f1 hello
  2884.     set x [eof $f1]
  2885.     flush $f1
  2886.     lappend x [eof $f1]
  2887.     gets $f1
  2888.     lappend x [eof $f1]
  2889.     gets $f1
  2890.     lappend x [eof $f1]
  2891.     close $f1
  2892.     set x
  2893. } {0 0 0 1}
  2894. test io-12.3 {Tcl_Eof with pipe} {stdio} {
  2895.     removeFile pipe
  2896.     set f1 [open pipe w]
  2897.     puts $f1 {gets stdin}
  2898.     puts $f1 {puts hello}
  2899.     close $f1
  2900.     set f1 [open "|[list $tcltest pipe]" r+]
  2901.     puts $f1 hello
  2902.     set x [eof $f1]
  2903.     flush $f1
  2904.     lappend x [eof $f1]
  2905.     gets $f1
  2906.     lappend x [eof $f1]
  2907.     gets $f1
  2908.     lappend x [eof $f1]
  2909.     gets $f1
  2910.     lappend x [eof $f1]
  2911.     gets $f1
  2912.     lappend x [eof $f1]
  2913.     close $f1
  2914.     set x
  2915. } {0 0 0 1 1 1}
  2916. test io-12.4 {Tcl_Eof, eof detection on nonblocking file} {nonBlockFiles} {
  2917.     removeFile test1
  2918.     set f [open test1 w]
  2919.     close $f
  2920.     set f [open test1 r]
  2921.     fconfigure $f -blocking off
  2922.     set l ""
  2923.     lappend l [gets $f]
  2924.     lappend l [eof $f]
  2925.     close $f
  2926.     set l
  2927. } {{} 1}
  2928. test io-12.5 {Tcl_Eof, eof detection on nonblocking pipe} {stdio} {
  2929.     removeFile pipe
  2930.     set f [open pipe w]
  2931.     puts $f {
  2932.     exit
  2933.     }
  2934.     close $f
  2935.     set f [open "|[list $tcltest pipe]" r]
  2936.     set l ""
  2937.     lappend l [gets $f]
  2938.     lappend l [eof $f]
  2939.     close $f
  2940.     set l
  2941. } {{} 1}
  2942. test io-12.6 {Tcl_Eof, eof char, lf write, auto read} {
  2943.     removeFile test1
  2944.     set f [open test1 w]
  2945.     fconfigure $f -translation lf -eofchar \x1a
  2946.     puts $f abc\ndef
  2947.     close $f
  2948.     set s [file size test1]
  2949.     set f [open test1 r]
  2950.     fconfigure $f -translation auto -eofchar \x1a
  2951.     set l [string length [read $f]]
  2952.     set e [eof $f]
  2953.     close $f
  2954.     list $s $l $e
  2955. } {9 8 1}
  2956. test io-12.7 {Tcl_Eof, eof char, lf write, lf read} {
  2957.     removeFile test1
  2958.     set f [open test1 w]
  2959.     fconfigure $f -translation lf -eofchar \x1a
  2960.     puts $f abc\ndef
  2961.     close $f
  2962.     set s [file size test1]
  2963.     set f [open test1 r]
  2964.     fconfigure $f -translation lf -eofchar \x1a
  2965.     set l [string length [read $f]]
  2966.     set e [eof $f]
  2967.     close $f
  2968.     list $s $l $e
  2969. } {9 8 1}
  2970. test io-12.8 {Tcl_Eof, eof char, cr write, auto read} {
  2971.     removeFile test1
  2972.     set f [open test1 w]
  2973.     fconfigure $f -translation cr -eofchar \x1a
  2974.     puts $f abc\ndef
  2975.     close $f
  2976.     set s [file size test1]
  2977.     set f [open test1 r]
  2978.     fconfigure $f -translation auto -eofchar \x1a
  2979.     set l [string length [read $f]]
  2980.     set e [eof $f]
  2981.     close $f
  2982.     list $s $l $e
  2983. } {9 8 1}
  2984. test io-12.9 {Tcl_Eof, eof char, cr write, cr read} {
  2985.     removeFile test1
  2986.     set f [open test1 w]
  2987.     fconfigure $f -translation cr -eofchar \x1a
  2988.     puts $f abc\ndef
  2989.     close $f
  2990.     set s [file size test1]
  2991.     set f [open test1 r]
  2992.     fconfigure $f -translation cr -eofchar \x1a
  2993.     set l [string length [read $f]]
  2994.     set e [eof $f]
  2995.     close $f
  2996.     list $s $l $e
  2997. } {9 8 1}
  2998. test io-12.10 {Tcl_Eof, eof char, crlf write, auto read} {
  2999.     removeFile test1
  3000.     set f [open test1 w]
  3001.     fconfigure $f -translation crlf -eofchar \x1a
  3002.     puts $f abc\ndef
  3003.     close $f
  3004.     set s [file size test1]
  3005.     set f [open test1 r]
  3006.     fconfigure $f -translation auto -eofchar \x1a
  3007.     set l [string length [read $f]]
  3008.     set e [eof $f]
  3009.     close $f
  3010.     list $s $l $e
  3011. } {11 8 1}
  3012. test io-12.11 {Tcl_Eof, eof char, crlf write, crlf read} {
  3013.     removeFile test1
  3014.     set f [open test1 w]
  3015.     fconfigure $f -translation crlf -eofchar \x1a
  3016.     puts $f abc\ndef
  3017.     close $f
  3018.     set s [file size test1]
  3019.     set f [open test1 r]
  3020.     fconfigure $f -translation crlf -eofchar \x1a
  3021.     set l [string length [read $f]]
  3022.     set e [eof $f]
  3023.     close $f
  3024.     list $s $l $e
  3025. } {11 8 1}
  3026. test io-12.12 {Tcl_Eof, eof char in middle, lf write, auto read} {
  3027.     removeFile test1
  3028.     set f [open test1 w]
  3029.     fconfigure $f -translation lf -eofchar {}
  3030.     set i [format abc\ndef\n%cqrs\nuvw 26]
  3031.     puts $f $i
  3032.     close $f
  3033.     set c [file size test1]
  3034.     set f [open test1 r]
  3035.     fconfigure $f -translation auto -eofchar \x1a
  3036.     set l [string length [read $f]]
  3037.     set e [eof $f]
  3038.     close $f
  3039.     list $c $l $e
  3040. } {17 8 1}
  3041. test io-12.13 {Tcl_Eof, eof char in middle, lf write, lf read} {
  3042.     removeFile test1
  3043.     set f [open test1 w]
  3044.     fconfigure $f -translation lf -eofchar {}
  3045.     set i [format abc\ndef\n%cqrs\nuvw 26]
  3046.     puts $f $i
  3047.     close $f
  3048.     set c [file size test1]
  3049.     set f [open test1 r]
  3050.     fconfigure $f -translation lf -eofchar \x1a
  3051.     set l [string length [read $f]]
  3052.     set e [eof $f]
  3053.     close $f
  3054.     list $c $l $e
  3055. } {17 8 1}
  3056. test io-12.14 {Tcl_Eof, eof char in middle, cr write, auto read} {
  3057.     removeFile test1
  3058.     set f [open test1 w]
  3059.     fconfigure $f -translation cr -eofchar {}
  3060.     set i [format abc\ndef\n%cqrs\nuvw 26]
  3061.     puts $f $i
  3062.     close $f
  3063.     set c [file size test1]
  3064.     set f [open test1 r]
  3065.     fconfigure $f -translation auto -eofchar \x1a
  3066.     set l [string length [read $f]]
  3067.     set e [eof $f]
  3068.     close $f
  3069.     list $c $l $e
  3070. } {17 8 1}
  3071. test io-12.15 {Tcl_Eof, eof char in middle, cr write, cr read} {
  3072.     removeFile test1
  3073.     set f [open test1 w]
  3074.     fconfigure $f -translation cr -eofchar {}
  3075.     set i [format abc\ndef\n%cqrs\nuvw 26]
  3076.     puts $f $i
  3077.     close $f
  3078.     set c [file size test1]
  3079.     set f [open test1 r]
  3080.     fconfigure $f -translation cr -eofchar \x1a
  3081.     set l [string length [read $f]]
  3082.     set e [eof $f]
  3083.     close $f
  3084.     list $c $l $e
  3085. } {17 8 1}
  3086. test io-12.16 {Tcl_Eof, eof char in middle, crlf write, auto read} {
  3087.     removeFile test1
  3088.     set f [open test1 w]
  3089.     fconfigure $f -translation crlf -eofchar {}
  3090.     set i [format abc\ndef\n%cqrs\nuvw 26]
  3091.     puts $f $i
  3092.     close $f
  3093.     set c [file size test1]
  3094.     set f [open test1 r]
  3095.     fconfigure $f -translation auto -eofchar \x1a
  3096.     set l [string length [read $f]]
  3097.     set e [eof $f]
  3098.     close $f
  3099.     list $c $l $e
  3100. } {21 8 1}
  3101. test io-12.17 {Tcl_Eof, eof char in middle, crlf write, crlf read} {
  3102.     removeFile test1
  3103.     set f [open test1 w]
  3104.     fconfigure $f -translation crlf -eofchar {}
  3105.     set i [format abc\ndef\n%cqrs\nuvw 26]
  3106.     puts $f $i
  3107.     close $f
  3108.     set c [file size test1]
  3109.     set f [open test1 r]
  3110.     fconfigure $f -translation crlf -eofchar \x1a
  3111.     set l [string length [read $f]]
  3112.     set e [eof $f]
  3113.     close $f
  3114.     list $c $l $e
  3115. } {21 8 1}
  3116.  
  3117. # Test Tcl_InputBlocked
  3118.  
  3119. test io-13.1 {Tcl_InputBlocked on nonblocking pipe} {unixOrPc tempNotPc} {
  3120.     set f1 [open "|[list $tcltest]" r+]
  3121.     puts $f1 {puts hello_from_pipe}
  3122.     flush $f1
  3123.     gets $f1
  3124.     fconfigure $f1 -blocking off -buffering full
  3125.     puts $f1 {puts hello}
  3126.     set x ""
  3127.     lappend x [gets $f1]
  3128.     lappend x [fblocked $f1]
  3129.     flush $f1
  3130.     after 200
  3131.     lappend x [gets $f1]
  3132.     lappend x [fblocked $f1]
  3133.     lappend x [gets $f1]
  3134.     lappend x [fblocked $f1]
  3135.     close $f1
  3136.     set x
  3137. } {{} 1 hello 0 {} 1}
  3138. test io-13.2 {Tcl_InputBlocked on blocking pipe} {unixOrPc tempNotPc} {
  3139.     set f1 [open "|[list $tcltest]" r+]
  3140.     fconfigure $f1 -buffering line
  3141.     puts $f1 {puts hello_from_pipe}
  3142.     set x ""
  3143.     lappend x [gets $f1]
  3144.     lappend x [fblocked $f1]
  3145.     puts $f1 {exit}
  3146.     lappend x [gets $f1]
  3147.     lappend x [fblocked $f1]
  3148.     lappend x [eof $f1]
  3149.     close $f1
  3150.     set x
  3151. } {hello_from_pipe 0 {} 0 1}
  3152. test io-13.3 {Tcl_InputBlocked vs files, short read} {
  3153.     removeFile test1
  3154.     set f [open test1 w]
  3155.     puts $f abcdefghijklmnop
  3156.     close $f
  3157.     set f [open test1 r]
  3158.     set l ""
  3159.     lappend l [fblocked $f]
  3160.     lappend l [read $f 3]
  3161.     lappend l [fblocked $f]
  3162.     lappend l [read -nonewline $f]
  3163.     lappend l [fblocked $f]
  3164.     lappend l [eof $f]
  3165.     close $f
  3166.     set l
  3167. } {0 abc 0 defghijklmnop 0 1}
  3168. test io-13.4 {Tcl_InputBlocked vs files, event driven read} {
  3169.     proc in {f} {
  3170.         global l x
  3171.     lappend l [read $f 3]
  3172.     if {[eof $f]} {lappend l eof; close $f; set x done}
  3173.     }
  3174.     removeFile test1
  3175.     set f [open test1 w]
  3176.     puts $f abcdefghijklmnop
  3177.     close $f
  3178.     set f [open test1 r]
  3179.     set l ""
  3180.     fileevent $f readable [list in $f]
  3181.     vwait x
  3182.     set l
  3183. } {abc def ghi jkl mno {p
  3184. } eof}
  3185. test io-13.5 {Tcl_InputBlocked vs files, short read, nonblocking} {nonBlockFiles} {
  3186.     removeFile test1
  3187.     set f [open test1 w]
  3188.     puts $f abcdefghijklmnop
  3189.     close $f
  3190.     set f [open test1 r]
  3191.     fconfigure $f -blocking off
  3192.     set l ""
  3193.     lappend l [fblocked $f]
  3194.     lappend l [read $f 3]
  3195.     lappend l [fblocked $f]
  3196.     lappend l [read -nonewline $f]
  3197.     lappend l [fblocked $f]
  3198.     lappend l [eof $f]
  3199.     close $f
  3200.     set l
  3201. } {0 abc 0 defghijklmnop 0 1}
  3202. test io-13.6 {Tcl_InputBlocked vs files, event driven read} {nonBlockFiles} {
  3203.     proc in {f} {
  3204.         global l x
  3205.     lappend l [read $f 3]
  3206.     if {[eof $f]} {lappend l eof; close $f; set x done}
  3207.     }
  3208.     removeFile test1
  3209.     set f [open test1 w]
  3210.     puts $f abcdefghijklmnop
  3211.     close $f
  3212.     set f [open test1 r]
  3213.     fconfigure $f -blocking off
  3214.     set l ""
  3215.     fileevent $f readable [list in $f]
  3216.     vwait x
  3217.     set l
  3218. } {abc def ghi jkl mno {p
  3219. } eof}
  3220.  
  3221. # Test Tcl_InputBuffered
  3222.  
  3223. test io-14.1 {Tcl_InputBuffered} {
  3224.     set f [open longfile r]
  3225.     fconfigure $f -buffersize 4096
  3226.     read $f 3
  3227.     set l ""
  3228.     lappend l [testchannel inputbuffered $f]
  3229.     lappend l [tell $f]
  3230.     close $f
  3231.     set l
  3232. } {4093 3}
  3233. test io-14.2 {Tcl_InputBuffered, test input flushing on seek} {
  3234.     set f [open longfile r]
  3235.     fconfigure $f -buffersize 4096
  3236.     read $f 3
  3237.     set l ""
  3238.     lappend l [testchannel inputbuffered $f]
  3239.     lappend l [tell $f]
  3240.     seek $f 0 current
  3241.     lappend l [testchannel inputbuffered $f]
  3242.     lappend l [tell $f]
  3243.     close $f
  3244.     set l
  3245. } {4093 3 0 3}
  3246.  
  3247. # Test Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize
  3248.  
  3249. test io-15.1 {Tcl_GetChannelBufferSize, default buffer size} {
  3250.     set f [open longfile r]
  3251.     set s [fconfigure $f -buffersize]
  3252.     close $f
  3253.     set s
  3254. } 4096
  3255. test io-15.2 {Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize} {
  3256.     set f [open longfile r]
  3257.     set l ""
  3258.     lappend l [fconfigure $f -buffersize]
  3259.     fconfigure $f -buffersize 10000
  3260.     lappend l [fconfigure $f -buffersize]
  3261.     fconfigure $f -buffersize 1
  3262.     lappend l [fconfigure $f -buffersize]
  3263.     fconfigure $f -buffersize -1
  3264.     lappend l [fconfigure $f -buffersize]
  3265.     fconfigure $f -buffersize 0
  3266.     lappend l [fconfigure $f -buffersize]
  3267.     fconfigure $f -buffersize 100000
  3268.     lappend l [fconfigure $f -buffersize]
  3269.     fconfigure $f -buffersize 10000000
  3270.     lappend l [fconfigure $f -buffersize]
  3271.     close $f
  3272.     set l
  3273. } {4096 10000 4096 4096 4096 100000 4096}
  3274.  
  3275. # Test Tcl_SetChannelOption, Tcl_GetChannelOption
  3276.  
  3277. test io-16.1 {Tcl_GetChannelOption} {
  3278.     removeFile test1
  3279.     set f1 [open test1 w]
  3280.     set x [fconfigure $f1 -blocking]
  3281.     close $f1
  3282.     set x
  3283. } 1
  3284. #
  3285. # Test 17.2 was removed.
  3286. #
  3287. test io-16.2 {Tcl_GetChannelOption} {
  3288.     removeFile test1
  3289.     set f1 [open test1 w]
  3290.     set x [fconfigure $f1 -buffering]
  3291.     close $f1
  3292.     set x
  3293. } full
  3294. test io-16.3 {Tcl_GetChannelOption} {
  3295.     removeFile test1
  3296.     set f1 [open test1 w]
  3297.     fconfigure $f1 -buffering line
  3298.     set x [fconfigure $f1 -buffering]
  3299.     close $f1
  3300.     set x
  3301. } line
  3302. test io-16.4 {Tcl_GetChannelOption, Tcl_SetChannelOption} {
  3303.     removeFile test1
  3304.     set f1 [open test1 w]
  3305.     set l ""
  3306.     lappend l [fconfigure $f1 -buffering]
  3307.     fconfigure $f1 -buffering line
  3308.     lappend l [fconfigure $f1 -buffering]
  3309.     fconfigure $f1 -buffering none
  3310.     lappend l [fconfigure $f1 -buffering]
  3311.     fconfigure $f1 -buffering line
  3312.     lappend l [fconfigure $f1 -buffering]
  3313.     fconfigure $f1 -buffering full
  3314.     lappend l [fconfigure $f1 -buffering]
  3315.     close $f1
  3316.     set l
  3317. } {full line none line full}
  3318. test io-16.5 {Tcl_GetChannelOption, invariance} {
  3319.     removeFile test1
  3320.     set f1 [open test1 w]
  3321.     set l ""
  3322.     lappend l [fconfigure $f1 -buffering]
  3323.     lappend l [list [catch {fconfigure $f1 -buffering green} msg] $msg]
  3324.     lappend l [fconfigure $f1 -buffering]
  3325.     close $f1
  3326.     set l
  3327. } {full {1 {bad value for -buffering: must be one of full, line, or none}} full}
  3328. test io-16.6 {Tcl_SetChannelOption, multiple options} {
  3329.     removeFile test1
  3330.     set f1 [open test1 w]
  3331.     fconfigure $f1 -translation lf -buffering line
  3332.     puts $f1 hello
  3333.     puts $f1 bye
  3334.     set x [file size test1]
  3335.     close $f1
  3336.     set x
  3337. } 10
  3338. test io-16.7 {Tcl_SetChannelOption, buffering, translation} {
  3339.     removeFile test1
  3340.     set f1 [open test1 w]
  3341.     fconfigure $f1 -translation lf
  3342.     puts $f1 hello
  3343.     puts $f1 bye
  3344.     set x ""
  3345.     fconfigure $f1 -buffering line
  3346.     lappend x [file size test1]
  3347.     puts $f1 really_bye
  3348.     lappend x [file size test1]
  3349.     close $f1
  3350.     set x
  3351. } {0 21}
  3352. test io-16.8 {Tcl_SetChannelOption, different buffering options} {
  3353.     removeFile test1
  3354.     set f1 [open test1 w]
  3355.     set l ""
  3356.     fconfigure $f1 -translation lf -buffering none -eofchar {}
  3357.     puts -nonewline $f1 hello
  3358.     lappend l [file size test1]
  3359.     puts -nonewline $f1 hello
  3360.     lappend l [file size test1]
  3361.     fconfigure $f1 -buffering full
  3362.     puts -nonewline $f1 hello
  3363.     lappend l [file size test1]
  3364.     fconfigure $f1 -buffering none
  3365.     lappend l [file size test1]
  3366.     puts -nonewline $f1 hello
  3367.     lappend l [file size test1]
  3368.     close $f1
  3369.     lappend l [file size test1]
  3370.     set l
  3371. } {5 10 10 10 20 20}
  3372. test io-16.9 {Tcl_SetChannelOption, blocking mode} {nonBlockFiles} {
  3373.     removeFile test1
  3374.     set f1 [open test1 w]
  3375.     close $f1
  3376.     set f1 [open test1 r]
  3377.     set x ""
  3378.     lappend x [fconfigure $f1 -blocking]
  3379.     fconfigure $f1 -blocking off
  3380.     lappend x [fconfigure $f1 -blocking]
  3381.     lappend x [gets $f1]
  3382.     lappend x [read $f1 1000]
  3383.     lappend x [fblocked $f1]
  3384.     lappend x [eof $f1]
  3385.     close $f1
  3386.     set x
  3387. } {1 0 {} {} 0 1}
  3388. test io-16.10 {Tcl_SetChannelOption, blocking mode} {unixOrPc tempNotPc} {
  3389.     removeFile pipe
  3390.     set f1 [open pipe w]
  3391.     puts $f1 {gets stdin}
  3392.     puts $f1 {after 100}
  3393.     puts $f1 {puts hi}
  3394.     puts $f1 {gets stdin}
  3395.     close $f1
  3396.     set x ""
  3397.     set f1 [open "|[list $tcltest pipe]" r+]
  3398.     fconfigure $f1 -blocking off -buffering line
  3399.     lappend x [fconfigure $f1 -blocking]
  3400.     lappend x [gets $f1]
  3401.     lappend x [fblocked $f1]
  3402.     puts $f1 hello
  3403.     lappend x [gets $f1]
  3404.     lappend x [fblocked $f1]
  3405.     puts $f1 bye
  3406.     lappend x [gets $f1]
  3407.     lappend x [fblocked $f1]
  3408.     fconfigure $f1 -blocking on
  3409.     lappend x [fconfigure $f1 -blocking]
  3410.     lappend x [gets $f1]
  3411.     lappend x [fblocked $f1]
  3412.     lappend x [eof $f1]
  3413.     lappend x [gets $f1]
  3414.     lappend x [eof $f1]
  3415.     close $f1
  3416.     set x
  3417. } {0 {} 1 {} 1 {} 1 1 hi 0 0 {} 1}
  3418. test io-16.11 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
  3419.     removeFile test1
  3420.     set f [open test1 w]
  3421.     fconfigure $f -buffersize -10
  3422.     set x [fconfigure $f -buffersize]
  3423.     close $f
  3424.     set x
  3425. } 4096
  3426. test io-16.12 {Tcl_SetChannelOption, Tcl_GetChannelOption buffer size} {
  3427.     removeFile test1
  3428.     set f [open test1 w]
  3429.     fconfigure $f -buffersize 10000000
  3430.     set x [fconfigure $f -buffersize]
  3431.     close $f
  3432.     set x
  3433. } 4096
  3434. test io-16.13 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
  3435.     removeFile test1
  3436.     set f [open test1 w]
  3437.     fconfigure $f -buffersize 40000
  3438.     set x [fconfigure $f -buffersize]
  3439.     close $f
  3440.     set x
  3441. } 40000
  3442.  
  3443. test io-17.1 {POSIX open access modes: RDWR} {
  3444.     removeFile test3
  3445.     set f [open test3 w]
  3446.     puts $f xyzzy
  3447.     close $f
  3448.     set f [open test3 RDWR]
  3449.     puts -nonewline $f "ab"
  3450.     seek $f 0 current
  3451.     set x [gets $f]
  3452.     close $f
  3453.     set f [open test3 r]
  3454.     lappend x [gets $f]
  3455.     close $f
  3456.     set x
  3457. } {zzy abzzy}
  3458. test io-17.2 {POSIX open access modes: CREAT} {unixOnly} {
  3459.     removeFile test3
  3460.     set f [open test3 {WRONLY CREAT} 0600]
  3461.     file stat test3 stats
  3462.     set x [format "0%o" [expr $stats(mode)&0777]]
  3463.     puts $f "line 1"
  3464.     close $f
  3465.     set f [open test3 r]
  3466.     lappend x [gets $f]
  3467.     close $f
  3468.     set x
  3469. } {0600 {line 1}}
  3470. test io-17.3 {POSIX open access modes: CREAT} {$testConfig(unix) && ([exec umask] == 2)} {
  3471.     # This test only works if your umask is 2, like ouster's.
  3472.     removeFile test3
  3473.     set f [open test3 {WRONLY CREAT}]
  3474.     close $f
  3475.     file stat test3 stats
  3476.     format "0%o" [expr $stats(mode)&0777]
  3477. } 0664
  3478. test io-17.4 {POSIX open access modes: CREAT} {
  3479.     removeFile test3
  3480.     set f [open test3 w]
  3481.     fconfigure $f -eofchar {}
  3482.     puts $f xyzzy
  3483.     close $f
  3484.     set f [open test3 {WRONLY CREAT}]
  3485.     fconfigure $f -eofchar {}
  3486.     puts -nonewline $f "ab"
  3487.     close $f
  3488.     set f [open test3 r]
  3489.     set x [gets $f]
  3490.     close $f
  3491.     set x
  3492. } abzzy
  3493. test io-17.5 {POSIX open access modes: APPEND} {
  3494.     removeFile test3
  3495.     set f [open test3 w]
  3496.     fconfigure $f -translation lf -eofchar {}
  3497.     puts $f xyzzy
  3498.     close $f
  3499.     set f [open test3 {WRONLY APPEND}]
  3500.     fconfigure $f -translation lf
  3501.     puts $f "new line"
  3502.     seek $f 0
  3503.     puts $f "abc"
  3504.     close $f
  3505.     set f [open test3 r]
  3506.     fconfigure $f -translation lf
  3507.     set x ""
  3508.     seek $f 6 current
  3509.     lappend x [gets $f]
  3510.     lappend x [gets $f]
  3511.     close $f
  3512.     set x
  3513. } {{new line} abc}
  3514. test io-17.6 {POSIX open access modes: EXCL} {
  3515.     removeFile test3
  3516.     set f [open test3 w]
  3517.     puts $f xyzzy
  3518.     close $f
  3519.     set msg [list [catch {open test3 {WRONLY CREAT EXCL}} msg] $msg]
  3520.     regsub " already " $msg " " msg
  3521.     string tolower $msg
  3522. } {1 {couldn't open "test3": file exists}}
  3523. test io-17.7 {POSIX open access modes: EXCL} {
  3524.     removeFile test3
  3525.     set f [open test3 {WRONLY CREAT EXCL}]
  3526.     fconfigure $f -eofchar {}
  3527.     puts $f "A test line"
  3528.     close $f
  3529.     viewFile test3
  3530. } {A test line}
  3531. test io-17.8 {POSIX open access modes: TRUNC} {
  3532.     removeFile test3
  3533.     set f [open test3 w]
  3534.     puts $f xyzzy
  3535.     close $f
  3536.     set f [open test3 {WRONLY TRUNC}]
  3537.     puts $f abc
  3538.     close $f
  3539.     set f [open test3 r]
  3540.     set x [gets $f]
  3541.     close $f
  3542.     set x
  3543. } abc
  3544. test io-17.9 {POSIX open access modes: NONBLOCK} {nonPortable macOrUnix} {
  3545.     removeFile test3
  3546.     set f [open test3 {WRONLY NONBLOCK CREAT}]
  3547.     puts $f "NONBLOCK test"
  3548.     close $f
  3549.     set f [open test3 r]
  3550.     set x [gets $f]
  3551.     close $f
  3552.     set x
  3553. } {NONBLOCK test}
  3554. test io-17.10 {POSIX open access modes: RDONLY} {
  3555.     set f [open test1 w]
  3556.     puts $f "two lines: this one"
  3557.     puts $f "and this"
  3558.     close $f
  3559.     set f [open test1 RDONLY]
  3560.     set x [list [gets $f] [catch {puts $f Test} msg] $msg]
  3561.     close $f
  3562.     string compare [string tolower $x] \
  3563.     [list {two lines: this one} 1 \
  3564.         [format "channel \"%s\" wasn't opened for writing" $f]]
  3565. } 0
  3566. test io-17.11 {POSIX open access modes: RDONLY} {
  3567.     removeFile test3
  3568.     string tolower [list [catch {open test3 RDONLY} msg] $msg]
  3569. } {1 {couldn't open "test3": no such file or directory}}
  3570. test io-17.12 {POSIX open access modes: WRONLY} {
  3571.     removeFile test3
  3572.     string tolower [list [catch {open test3 WRONLY} msg] $msg]
  3573. } {1 {couldn't open "test3": no such file or directory}}
  3574. test io-17.13 {POSIX open access modes: WRONLY} {
  3575.     makeFile xyzzy test3
  3576.     set f [open test3 WRONLY]
  3577.     fconfigure $f -eofchar {}
  3578.     puts -nonewline $f "ab"
  3579.     seek $f 0 current
  3580.     set x [list [catch {gets $f} msg] $msg]
  3581.     close $f
  3582.     lappend x [viewFile test3]
  3583.     string compare [string tolower $x] \
  3584.     [list 1 "channel \"$f\" wasn't opened for reading" abzzy]
  3585. } 0
  3586. test io-17.14 {POSIX open access modes: RDWR} {
  3587.     removeFile test3
  3588.     string tolower [list [catch {open test3 RDWR} msg] $msg]
  3589. } {1 {couldn't open "test3": no such file or directory}}
  3590. test io-17.15 {POSIX open access modes: RDWR} {
  3591.     makeFile xyzzy test3
  3592.     set f [open test3 RDWR]
  3593.     puts -nonewline $f "ab"
  3594.     seek $f 0 current
  3595.     set x [gets $f]
  3596.     close $f
  3597.     lappend x [viewFile test3]
  3598. } {zzy abzzy}
  3599. if {![file exists ~/_test_] && [file writable ~]} {
  3600.     test io-17.16 {tilde substitution in open} {
  3601.     set f [open ~/_test_ w]
  3602.     puts $f "Some text"
  3603.     close $f
  3604.     set x [file exists [file join $env(HOME) _test_]]
  3605.     removeFile [file join $env(HOME) _test_]
  3606.     set x
  3607.     } 1
  3608. }
  3609. test io-17.17 {tilde substitution in open} {
  3610.     set home $env(HOME)
  3611.     unset env(HOME)
  3612.     set x [list [catch {open ~/foo} msg] $msg]
  3613.     set env(HOME) $home
  3614.     set x
  3615. } {1 {couldn't find HOME environment variable to expand path}}
  3616.  
  3617. test io-18.1 {Tcl_FileeventCmd: errors} {
  3618.     list [catch {fileevent foo} msg] $msg
  3619. } {1 {wrong # args: must be "fileevent channelId event ?script?}}
  3620. test io-18.2 {Tcl_FileeventCmd: errors} {
  3621.     list [catch {fileevent foo bar baz q} msg] $msg
  3622. } {1 {wrong # args: must be "fileevent channelId event ?script?}}
  3623. test io-18.3 {Tcl_FileeventCmd: errors} {
  3624.     list [catch {fileevent gorp readable} msg] $msg
  3625. } {1 {can not find channel named "gorp"}}
  3626. test io-18.4 {Tcl_FileeventCmd: errors} {
  3627.     list [catch {fileevent gorp writable} msg] $msg
  3628. } {1 {can not find channel named "gorp"}}
  3629. test io-18.5 {Tcl_FileeventCmd: errors} {
  3630.     list [catch {fileevent gorp who-knows} msg] $msg
  3631. } {1 {bad event name "who-knows": must be readable or writable}}
  3632.  
  3633. #
  3634. # Test fileevent on a file
  3635. #
  3636.  
  3637. set f [open foo w+]
  3638.  
  3639. test io-19.1 {Tcl_FileeventCmd: creating, deleting, querying} {
  3640.     list [fileevent $f readable] [fileevent $f writable]
  3641. } {{} {}}
  3642. test io-19.2 {Tcl_FileeventCmd: replacing} {
  3643.     set result {}
  3644.     fileevent $f r "first script"
  3645.     lappend result [fileevent $f readable]
  3646.     fileevent $f r "new script"
  3647.     lappend result [fileevent $f readable]
  3648.     fileevent $f r "yet another"
  3649.     lappend result [fileevent $f readable]
  3650.     fileevent $f r ""
  3651.     lappend result [fileevent $f readable]
  3652. } {{first script} {new script} {yet another} {}}
  3653.  
  3654. #
  3655. # Test fileevent on a pipe
  3656. #
  3657.  
  3658. if {($tcl_platform(platform) != "macintosh") && \
  3659.     ($testConfig(unixExecs) == 1)} {
  3660.  
  3661. catch {set f2 [open "|[list cat -u]" r+]}
  3662. catch {set f3 [open "|[list cat -u]" r+]}
  3663.  
  3664. test io-20.1 {Tcl_FileeventCmd: creating, deleting, querying} {
  3665.     set result {}
  3666.     fileevent $f readable "script 1"
  3667.     lappend result [fileevent $f readable] [fileevent $f writable]
  3668.     fileevent $f writable "write script"
  3669.     lappend result [fileevent $f readable] [fileevent $f writable]
  3670.     fileevent $f readable {}
  3671.     lappend result [fileevent $f readable] [fileevent $f writable]
  3672.     fileevent $f writable {}
  3673.     lappend result [fileevent $f readable] [fileevent $f writable]
  3674. } {{script 1} {} {script 1} {write script} {} {write script} {} {}}
  3675. test io-20.2 {Tcl_FileeventCmd: deleting when many present} {
  3676.     set result {}
  3677.     lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
  3678.     fileevent $f r "read f"
  3679.     fileevent $f2 r "read f2"
  3680.     fileevent $f3 r "read f3"
  3681.     lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
  3682.     fileevent $f2 r {}
  3683.     lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
  3684.     fileevent $f3 r {}
  3685.     lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
  3686.     fileevent $f r {}
  3687.     lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
  3688. } {{} {} {} {read f} {read f2} {read f3} {read f} {} {read f3} {read f} {} {} {} {} {}}
  3689.  
  3690. test io-21.1 {FileEventProc procedure: normal read event} {
  3691.     fileevent $f2 readable {
  3692.     set x [gets $f2]; fileevent $f2 readable {}
  3693.     }
  3694.     puts $f2 text; flush $f2
  3695.     set x initial
  3696.     vwait x
  3697.     set x
  3698. } {text}
  3699. test io-21.2 {FileEventProc procedure: error in read event} {
  3700.     proc bgerror args {
  3701.     global x
  3702.     set x $args
  3703.     }
  3704.     fileevent $f2 readable {error bogus}
  3705.     puts $f2 text; flush $f2
  3706.     set x initial
  3707.     vwait x
  3708.     rename bgerror {}
  3709.     list $x [fileevent $f2 readable]
  3710. } {bogus {}}
  3711. test io-21.3 {FileEventProc procedure: normal write event} {
  3712.     fileevent $f2 writable {
  3713.     lappend x "triggered"
  3714.     incr count -1
  3715.     if {$count <= 0} {
  3716.         fileevent $f2 writable {}
  3717.     }
  3718.     }
  3719.     set x initial
  3720.     set count 3
  3721.     vwait x
  3722.     vwait x
  3723.     vwait x
  3724.     set x
  3725. } {initial triggered triggered triggered}
  3726. test io-21.4 {FileEventProc procedure: eror in write event} {
  3727.     proc bgerror args {
  3728.     global x
  3729.     set x $args
  3730.     }
  3731.     fileevent $f2 writable {error bad-write}
  3732.     set x initial
  3733.     vwait x
  3734.     rename bgerror {}
  3735.     list $x [fileevent $f2 writable]
  3736. } {bad-write {}}
  3737. test io-21.5 {FileEventProc procedure: end of file} {unixOrPc} {
  3738.     set f4 [open "|[list $tcltest cat << foo]" r]
  3739.     fileevent $f4 readable {
  3740.     if {[gets $f4 line] < 0} {
  3741.         lappend x eof
  3742.         fileevent $f4 readable {}
  3743.     } else {
  3744.         lappend x $line
  3745.     }
  3746.     }
  3747.     set x initial
  3748.     vwait x
  3749.     vwait x
  3750.     close $f4
  3751.     set x
  3752. } {initial foo eof}
  3753.  
  3754. catch {close $f2}
  3755. catch {close $f3}
  3756.  
  3757. }
  3758.     # Closes if {($platform(platform) != "macintosh") && \
  3759.     #        ($testConfig(unixExecs) == 1)} clause
  3760.  
  3761. close $f
  3762. makeFile "foo bar" foo
  3763. test io-22.1 {DeleteFileEvent, cleanup on close} {
  3764.     set f [open foo r]
  3765.     fileevent $f readable {
  3766.     lappend x "binding triggered: \"[gets $f]\""
  3767.     fileevent $f readable {}
  3768.     }
  3769.     close $f
  3770.     set x initial
  3771.     after 100 { set y done }
  3772.     vwait y
  3773.     set x
  3774. } {initial}
  3775. test io-22.2 {DeleteFileEvent, cleanup on close} {
  3776.     set f [open foo r]
  3777.     set f2 [open foo r]
  3778.     fileevent $f readable {
  3779.         lappend x "f triggered: \"[gets $f]\""
  3780.         fileevent $f readable {}
  3781.     }
  3782.     fileevent $f2 readable {
  3783.     lappend x "f2 triggered: \"[gets $f2]\""
  3784.     fileevent $f2 readable {}
  3785.     }
  3786.     close $f
  3787.     set x initial
  3788.     vwait x
  3789.     close $f2
  3790.     set x
  3791. } {initial {f2 triggered: "foo bar"}}
  3792. test io-22.3 {DeleteFileEvent, cleanup on close} {
  3793.     set f [open foo r]
  3794.     set f2 [open foo r]
  3795.     set f3 [open foo r]
  3796.     fileevent $f readable {f script}
  3797.     fileevent $f2 readable {f2 script}
  3798.     fileevent $f3 readable {f3 script}
  3799.     set x {}
  3800.     close $f2
  3801.     lappend x [catch {fileevent $f readable} msg] $msg \
  3802.         [catch {fileevent $f2 readable}] \
  3803.         [catch {fileevent $f3 readable} msg] $msg
  3804.     close $f3
  3805.     lappend x [catch {fileevent $f readable} msg] $msg \
  3806.         [catch {fileevent $f2 readable}] \
  3807.         [catch {fileevent $f3 readable}]
  3808.     close $f
  3809.     lappend x [catch {fileevent $f readable}] \
  3810.         [catch {fileevent $f2 readable}] \
  3811.         [catch {fileevent $f3 readable}]
  3812. } {0 {f script} 1 0 {f3 script} 0 {f script} 1 1 1 1 1}
  3813.  
  3814. # Execute these tests only if the "testfevent" command is present.
  3815.  
  3816. if {[info commands testfevent] == "testfevent"} {
  3817.  
  3818. test io-23.1 {Tcl event loop vs multiple interpreters} {
  3819.     testfevent create
  3820.     testfevent cmd {
  3821.         set f [open foo r]
  3822.         set x "no event"
  3823.         fileevent $f readable {
  3824.             set x "f triggered: [gets $f]"
  3825.             fileevent $f readable {}
  3826.         }
  3827.     } 
  3828.     after 1    ;# We must delay because Windows takes a little time to notice
  3829.     update
  3830.     testfevent cmd {close $f}
  3831.     list [testfevent cmd {set x}] [testfevent cmd {info commands after}]
  3832. } {{f triggered: foo bar} after}
  3833. test io-23.2 {Tcl event loop vs multiple interpreters} {
  3834.     testfevent create
  3835.     testfevent cmd {
  3836.         set x 0
  3837.         after 100 {set x triggered}
  3838.         vwait x
  3839.         set x
  3840.     }
  3841. } {triggered}
  3842. test io-23.3 {Tcl event loop vs multiple interpreters} {
  3843.     testfevent create
  3844.     testfevent cmd {
  3845.         set x 0
  3846.         after 10 {lappend x timer}
  3847.         after 30
  3848.         set result $x
  3849.         update idletasks
  3850.         lappend result $x
  3851.         update
  3852.         lappend result $x
  3853.     }
  3854. } {0 0 {0 timer}}
  3855.  
  3856. test io-24.1 {fileevent vs multiple interpreters} {
  3857.     set f [open foo r]
  3858.     set f2 [open foo r]
  3859.     set f3 [open foo r]
  3860.     fileevent $f readable {script 1}
  3861.     testfevent create
  3862.     testfevent share $f2
  3863.     testfevent cmd "fileevent $f2 readable {script 2}"
  3864.     fileevent $f3 readable {sript 3}
  3865.     set x {}
  3866.     lappend x [fileevent $f2 readable]
  3867.     testfevent delete
  3868.     lappend x [fileevent $f readable] [fileevent $f2 readable] \
  3869.         [fileevent $f3 readable]
  3870.     close $f
  3871.     close $f2
  3872.     close $f3
  3873.     set x
  3874. } {{} {script 1} {} {sript 3}}
  3875. test io-24.2 {deleting fileevent on interpreter delete} {
  3876.     set f [open foo r]
  3877.     set f2 [open foo r]
  3878.     set f3 [open foo r]
  3879.     set f4 [open foo r]
  3880.     fileevent $f readable {script 1}
  3881.     testfevent create
  3882.     testfevent share $f2
  3883.     testfevent share $f3
  3884.     testfevent cmd "fileevent $f2 readable {script 2}
  3885.         fileevent $f3 readable {script 3}"
  3886.     fileevent $f4 readable {script 4}
  3887.     testfevent delete
  3888.     set x [list [fileevent $f readable] [fileevent $f2 readable] \
  3889.                 [fileevent $f3 readable] [fileevent $f4 readable]]
  3890.     close $f
  3891.     close $f2
  3892.     close $f3
  3893.     close $f4
  3894.     set x
  3895. } {{script 1} {} {} {script 4}}
  3896. test io-24.3 {deleting fileevent on interpreter delete} {
  3897.     set f [open foo r]
  3898.     set f2 [open foo r]
  3899.     set f3 [open foo r]
  3900.     set f4 [open foo r]
  3901.     testfevent create
  3902.     testfevent share $f3
  3903.     testfevent share $f4
  3904.     fileevent $f readable {script 1}
  3905.     fileevent $f2 readable {script 2}
  3906.     testfevent cmd "fileevent $f3 readable {script 3}
  3907.       fileevent $f4 readable {script 4}"
  3908.     testfevent delete
  3909.     set x [list [fileevent $f readable] [fileevent $f2 readable] \
  3910.                 [fileevent $f3 readable] [fileevent $f4 readable]]
  3911.     close $f
  3912.     close $f2
  3913.     close $f3
  3914.     close $f4
  3915.     set x
  3916. } {{script 1} {script 2} {} {}}
  3917. test io-24.4 {file events on shared files and multiple interpreters} {
  3918.     set f [open foo r]
  3919.     set f2 [open foo r]
  3920.     testfevent create
  3921.     testfevent share $f
  3922.     testfevent cmd "fileevent $f readable {script 1}"
  3923.     fileevent $f readable {script 2}
  3924.     fileevent $f2 readable {script 3}
  3925.     set x [list [fileevent $f2 readable] \
  3926.                 [testfevent cmd "fileevent $f readable"] \
  3927.                 [fileevent $f readable]]
  3928.     testfevent delete
  3929.     close $f
  3930.     close $f2
  3931.     set x
  3932. } {{script 3} {script 1} {script 2}}
  3933. test io-24.5 {file events on shared files, deleting file events} {
  3934.     set f [open foo r]
  3935.     testfevent create
  3936.     testfevent share $f
  3937.     testfevent cmd "fileevent $f readable {script 1}"
  3938.     fileevent $f readable {script 2}
  3939.     testfevent cmd "fileevent $f readable {}"
  3940.     set x [list [testfevent cmd "fileevent $f readable"] \
  3941.                 [fileevent $f readable]]
  3942.     testfevent delete
  3943.     close $f
  3944.     set x
  3945. } {{} {script 2}}
  3946. test io-24.6 {file events on shared files, deleting file events} {
  3947.     set f [open foo r]
  3948.     testfevent create
  3949.     testfevent share $f
  3950.     testfevent cmd "fileevent $f readable {script 1}"
  3951.     fileevent $f readable {script 2}
  3952.     fileevent $f readable {}
  3953.     set x [list [testfevent cmd "fileevent $f readable"] \
  3954.                 [fileevent $f readable]]
  3955.     testfevent delete
  3956.     close $f
  3957.     set x
  3958. } {{script 1} {}}
  3959.  
  3960. }
  3961.  
  3962. # The above curly closes the test for presence of the "testfevent" command.
  3963.  
  3964. test io-25.1 {testing readability conditions} {
  3965.     set f [open bar w]
  3966.     puts $f abcdefg
  3967.     puts $f abcdefg
  3968.     puts $f abcdefg
  3969.     puts $f abcdefg
  3970.     puts $f abcdefg
  3971.     close $f
  3972.     set f [open bar r]
  3973.     fileevent $f readable [list consume $f]
  3974.     proc consume {f} {
  3975.     global x l
  3976.     lappend l called
  3977.     if {[eof $f]} {
  3978.         close $f
  3979.         set x done
  3980.     } else {
  3981.         gets $f
  3982.     }
  3983.     }
  3984.     set l ""
  3985.     set x not_done
  3986.     vwait x
  3987.     list $x $l
  3988. } {done {called called called called called called called}}
  3989. test io-25.2 {testing readability conditions} {nonBlockFiles} {
  3990.     set f [open bar w]
  3991.     puts $f abcdefg
  3992.     puts $f abcdefg
  3993.     puts $f abcdefg
  3994.     puts $f abcdefg
  3995.     puts $f abcdefg
  3996.     close $f
  3997.     set f [open bar r]
  3998.     fileevent $f readable [list consume $f]
  3999.     fconfigure $f -blocking off
  4000.     proc consume {f} {
  4001.     global x l
  4002.     lappend l called
  4003.     if {[eof $f]} {
  4004.         close $f
  4005.         set x done
  4006.     } else {
  4007.         gets $f
  4008.     }
  4009.     }
  4010.     set l ""
  4011.     set x not_done
  4012.     vwait x
  4013.     list $x $l
  4014. } {done {called called called called called called called}}
  4015. test io-25.3 {testing readability conditions} {unixOnly nonBlockFiles} {
  4016.     set f [open bar w]
  4017.     puts $f abcdefg
  4018.     puts $f abcdefg
  4019.     puts $f abcdefg
  4020.     puts $f abcdefg
  4021.     puts $f abcdefg
  4022.     close $f
  4023.     set f [open my_script w]
  4024.     puts $f {
  4025.     proc copy_slowly {f} {
  4026.         while {![eof $f]} {
  4027.         puts [gets $f]
  4028.         after 200
  4029.         }
  4030.         close $f
  4031.     }
  4032.     }
  4033.     close $f
  4034.     set f [open "|[list $tcltest]" r+]
  4035.     fileevent $f readable [list consume $f]
  4036.     fconfigure $f -buffering line
  4037.     fconfigure $f -blocking off
  4038.     proc consume {f} {
  4039.     global x l
  4040.     if {[eof $f]} {
  4041.         set x done
  4042.     } else {
  4043.         gets $f
  4044.         lappend l [fblocked $f]
  4045.         gets $f
  4046.         lappend l [fblocked $f]
  4047.     }
  4048.     }
  4049.     set l ""
  4050.     set x not_done
  4051.     puts $f {source my_script}
  4052.     puts $f {set f [open bar r]}
  4053.     puts $f {copy_slowly $f}
  4054.     puts $f {exit}
  4055.     vwait x
  4056.     close $f
  4057.     list $x $l
  4058. } {done {0 1 0 1 0 1 0 1 0 1 0 1 0 0}}
  4059. test io-25.4 {lf write, testing readability, ^Z termination, auto read mode} {
  4060.     removeFile test1
  4061.     set f [open test1 w]
  4062.     fconfigure $f -translation lf
  4063.     set c [format "abc\ndef\n%c" 26]
  4064.     puts -nonewline $f $c
  4065.     close $f
  4066.     proc consume {f} {
  4067.     global c x l
  4068.     if {[eof $f]} {
  4069.        set x done
  4070.        close $f
  4071.     } else {
  4072.        lappend l [gets $f]
  4073.        incr c
  4074.     }
  4075.     }
  4076.     set c 0
  4077.     set l ""
  4078.     set f [open test1 r]
  4079.     fconfigure $f -translation auto -eofchar \x1a
  4080.     fileevent $f readable [list consume $f]
  4081.     vwait x
  4082.     list $c $l
  4083. } {3 {abc def {}}}
  4084. test io-25.5 {lf write, testing readability, ^Z in middle, auto read mode} {
  4085.     removeFile test1
  4086.     set f [open test1 w]
  4087.     fconfigure $f -translation lf
  4088.     set c [format "abc\ndef\n%cfoo\nbar\n" 26]
  4089.     puts -nonewline $f $c
  4090.     close $f
  4091.     proc consume {f} {
  4092.     global c x l
  4093.     if {[eof $f]} {
  4094.        set x done
  4095.        close $f
  4096.     } else {
  4097.        lappend l [gets $f]
  4098.        incr c
  4099.     }
  4100.     }
  4101.     set c 0
  4102.     set l ""
  4103.     set f [open test1 r]
  4104.     fconfigure $f -eofchar \x1a -translation auto
  4105.     fileevent $f readable [list consume $f]
  4106.     vwait x
  4107.     list $c $l
  4108. } {3 {abc def {}}}
  4109. test io-25.6 {cr write, testing readability, ^Z termination, auto read mode} {
  4110.     removeFile test1
  4111.     set f [open test1 w]
  4112.     fconfigure $f -translation cr
  4113.     set c [format "abc\ndef\n%c" 26]
  4114.     puts -nonewline $f $c
  4115.     close $f
  4116.     proc consume {f} {
  4117.     global c x l
  4118.     if {[eof $f]} {
  4119.        set x done
  4120.        close $f
  4121.     } else {
  4122.        lappend l [gets $f]
  4123.        incr c
  4124.     }
  4125.     }
  4126.     set c 0
  4127.     set l ""
  4128.     set f [open test1 r]
  4129.     fconfigure $f -translation auto -eofchar \x1a
  4130.     fileevent $f readable [list consume $f]
  4131.     vwait x
  4132.     list $c $l
  4133. } {3 {abc def {}}}
  4134. test io-25.7 {cr write, testing readability, ^Z in middle, auto read mode} {
  4135.     removeFile test1
  4136.     set f [open test1 w]
  4137.     fconfigure $f -translation cr
  4138.     set c [format "abc\ndef\n%cfoo\nbar\n" 26]
  4139.     puts -nonewline $f $c
  4140.     close $f
  4141.     proc consume {f} {
  4142.     global c x l
  4143.     if {[eof $f]} {
  4144.        set x done
  4145.        close $f
  4146.     } else {
  4147.        lappend l [gets $f]
  4148.        incr c
  4149.     }
  4150.     }
  4151.     set c 0
  4152.     set l ""
  4153.     set f [open test1 r]
  4154.     fconfigure $f -eofchar \x1a -translation auto
  4155.     fileevent $f readable [list consume $f]
  4156.     vwait x
  4157.     list $c $l
  4158. } {3 {abc def {}}}
  4159. test io-25.8 {crlf write, testing readability, ^Z termination, auto read mode} {
  4160.     removeFile test1
  4161.     set f [open test1 w]
  4162.     fconfigure $f -translation crlf
  4163.     set c [format "abc\ndef\n%c" 26]
  4164.     puts -nonewline $f $c
  4165.     close $f
  4166.     proc consume {f} {
  4167.     global c x l
  4168.     if {[eof $f]} {
  4169.        set x done
  4170.        close $f
  4171.     } else {
  4172.        lappend l [gets $f]
  4173.        incr c
  4174.     }
  4175.     }
  4176.     set c 0
  4177.     set l ""
  4178.     set f [open test1 r]
  4179.     fconfigure $f -translation auto -eofchar \x1a
  4180.     fileevent $f readable [list consume $f]
  4181.     vwait x
  4182.     list $c $l
  4183. } {3 {abc def {}}}
  4184. test io-25.9 {crlf write, testing readability, ^Z in middle, auto read mode} {
  4185.     removeFile test1
  4186.     set f [open test1 w]
  4187.     fconfigure $f -translation crlf
  4188.     set c [format "abc\ndef\n%cfoo\nbar\n" 26]
  4189.     puts -nonewline $f $c
  4190.     close $f
  4191.     proc consume {f} {
  4192.     global c x l
  4193.     if {[eof $f]} {
  4194.        set x done
  4195.        close $f
  4196.     } else {
  4197.        lappend l [gets $f]
  4198.        incr c
  4199.     }
  4200.     }
  4201.     set c 0
  4202.     set l ""
  4203.     set f [open test1 r]
  4204.     fconfigure $f -eofchar \x1a -translation auto
  4205.     fileevent $f readable [list consume $f]
  4206.     vwait x
  4207.     list $c $l
  4208. } {3 {abc def {}}}
  4209. test io-25.10 {lf write, testing readability, ^Z in middle, lf read mode} {
  4210.     removeFile test1
  4211.     set f [open test1 w]
  4212.     fconfigure $f -translation lf
  4213.     set c [format "abc\ndef\n%cfoo\nbar\n" 26]
  4214.     puts -nonewline $f $c
  4215.     close $f
  4216.     proc consume {f} {
  4217.     global c x l
  4218.     if {[eof $f]} {
  4219.        set x done
  4220.        close $f
  4221.     } else {
  4222.        lappend l [gets $f]
  4223.        incr c
  4224.     }
  4225.     }
  4226.     set c 0
  4227.     set l ""
  4228.     set f [open test1 r]
  4229.     fconfigure $f -eofchar \x1a -translation lf
  4230.     fileevent $f readable [list consume $f]
  4231.     vwait x
  4232.     list $c $l
  4233. } {3 {abc def {}}}
  4234. test io-25.11 {lf write, testing readability, ^Z termination, lf read mode} {
  4235.     removeFile test1
  4236.     set f [open test1 w]
  4237.     fconfigure $f -translation lf
  4238.     set c [format "abc\ndef\n%c" 26]
  4239.     puts -nonewline $f $c
  4240.     close $f
  4241.     proc consume {f} {
  4242.     global c x l
  4243.     if {[eof $f]} {
  4244.        set x done
  4245.        close $f
  4246.     } else {
  4247.        lappend l [gets $f]
  4248.        incr c
  4249.     }
  4250.     }
  4251.     set c 0
  4252.     set l ""
  4253.     set f [open test1 r]
  4254.     fconfigure $f -translation lf -eofchar \x1a
  4255.     fileevent $f readable [list consume $f]
  4256.     vwait x
  4257.     list $c $l
  4258. } {3 {abc def {}}}
  4259. test io-25.12 {cr write, testing readability, ^Z in middle, cr read mode} {
  4260.     removeFile test1
  4261.     set f [open test1 w]
  4262.     fconfigure $f -translation cr
  4263.     set c [format "abc\ndef\n%cfoo\nbar\n" 26]
  4264.     puts -nonewline $f $c
  4265.     close $f
  4266.     proc consume {f} {
  4267.     global c x l
  4268.     if {[eof $f]} {
  4269.        set x done
  4270.        close $f
  4271.     } else {
  4272.        lappend l [gets $f]
  4273.        incr c
  4274.     }
  4275.     }
  4276.     set c 0
  4277.     set l ""
  4278.     set f [open test1 r]
  4279.     fconfigure $f -eofchar \x1a -translation cr
  4280.     fileevent $f readable [list consume $f]
  4281.     vwait x
  4282.     list $c $l
  4283. } {3 {abc def {}}}
  4284. test io-25.13 {cr write, testing readability, ^Z termination, cr read mode} {
  4285.     removeFile test1
  4286.     set f [open test1 w]
  4287.     fconfigure $f -translation cr
  4288.     set c [format "abc\ndef\n%c" 26]
  4289.     puts -nonewline $f $c
  4290.     close $f
  4291.     proc consume {f} {
  4292.     global c x l
  4293.     if {[eof $f]} {
  4294.        set x done
  4295.        close $f
  4296.     } else {
  4297.        lappend l [gets $f]
  4298.        incr c
  4299.     }
  4300.     }
  4301.     set c 0
  4302.     set l ""
  4303.     set f [open test1 r]
  4304.     fconfigure $f -translation cr -eofchar \x1a
  4305.     fileevent $f readable [list consume $f]
  4306.     vwait x
  4307.     list $c $l
  4308. } {3 {abc def {}}}
  4309. test io-25.14 {crlf write, testing readability, ^Z in middle, crlf read mode} {
  4310.     removeFile test1
  4311.     set f [open test1 w]
  4312.     fconfigure $f -translation crlf
  4313.     set c [format "abc\ndef\n%cfoo\nbar\n" 26]
  4314.     puts -nonewline $f $c
  4315.     close $f
  4316.     proc consume {f} {
  4317.     global c x l
  4318.     if {[eof $f]} {
  4319.        set x done
  4320.        close $f
  4321.     } else {
  4322.        lappend l [gets $f]
  4323.        incr c
  4324.     }
  4325.     }
  4326.     set c 0
  4327.     set l ""
  4328.     set f [open test1 r]
  4329.     fconfigure $f -eofchar \x1a -translation crlf
  4330.     fileevent $f readable [list consume $f]
  4331.     vwait x
  4332.     list $c $l
  4333. } {3 {abc def {}}}
  4334. test io-25.15 {crlf write, testing readability, ^Z termi, crlf read mode} {
  4335.     removeFile test1
  4336.     set f [open test1 w]
  4337.     fconfigure $f -translation crlf
  4338.     set c [format "abc\ndef\n%c" 26]
  4339.     puts -nonewline $f $c
  4340.     close $f
  4341.     proc consume {f} {
  4342.     global c x l
  4343.     if {[eof $f]} {
  4344.        set x done
  4345.        close $f
  4346.     } else {
  4347.        lappend l [gets $f]
  4348.        incr c
  4349.     }
  4350.     }
  4351.     set c 0
  4352.     set l ""
  4353.     set f [open test1 r]
  4354.     fconfigure $f -translation crlf -eofchar \x1a
  4355.     fileevent $f readable [list consume $f]
  4356.     vwait x
  4357.     list $c $l
  4358. } {3 {abc def {}}}
  4359.  
  4360. test io-26.1 {testing crlf reading, leftover cr disgorgment} {
  4361.     removeFile test1
  4362.     set f [open test1 w]
  4363.     fconfigure $f -translation lf
  4364.     puts -nonewline $f "a\rb\rc\r\n"
  4365.     close $f
  4366.     set f [open test1 r]
  4367.     set l ""
  4368.     lappend l [file size test1]
  4369.     fconfigure $f -translation crlf
  4370.     lappend l [read $f 1]
  4371.     lappend l [tell $f]
  4372.     lappend l [read $f 1]
  4373.     lappend l [tell $f]
  4374.     lappend l [read $f 1]
  4375.     lappend l [tell $f]
  4376.     lappend l [read $f 1]
  4377.     lappend l [tell $f]
  4378.     lappend l [read $f 1]
  4379.     lappend l [tell $f]
  4380.     lappend l [read $f 1]
  4381.     lappend l [tell $f]
  4382.     lappend l [eof $f]
  4383.     lappend l [read $f 1]
  4384.     lappend l [eof $f]
  4385.     close $f
  4386.     set l
  4387. } "7 a 1 [list \r] 2 b 3 [list \r] 4 c 5 {
  4388. } 7 0 {} 1"
  4389. test io-26.2 {testing crlf reading, leftover cr disgorgment} {
  4390.     removeFile test1
  4391.     set f [open test1 w]
  4392.     fconfigure $f -translation lf
  4393.     puts -nonewline $f "a\rb\rc\r\n"
  4394.     close $f
  4395.     set f [open test1 r]
  4396.     set l ""
  4397.     lappend l [file size test1]
  4398.     fconfigure $f -translation crlf
  4399.     lappend l [read $f 2]
  4400.     lappend l [tell $f]
  4401.     lappend l [read $f 2]
  4402.     lappend l [tell $f]
  4403.     lappend l [read $f 2]
  4404.     lappend l [tell $f]
  4405.     lappend l [eof $f]
  4406.     lappend l [read $f 2]
  4407.     lappend l [tell $f]
  4408.     lappend l [eof $f]
  4409.     close $f
  4410.     set l
  4411. } "7 [list a\r] 2 [list b\r] 4 [list c\n] 7 0 {} 7 1"
  4412. test io-26.3 {testing crlf reading, leftover cr disgorgment} {
  4413.     removeFile test1
  4414.     set f [open test1 w]
  4415.     fconfigure $f -translation lf
  4416.     puts -nonewline $f "a\rb\rc\r\n"
  4417.     close $f
  4418.     set f [open test1 r]
  4419.     set l ""
  4420.     lappend l [file size test1]
  4421.     fconfigure $f -translation crlf
  4422.     lappend l [read $f 3]
  4423.     lappend l [tell $f]
  4424.     lappend l [read $f 3]
  4425.     lappend l [tell $f]
  4426.     lappend l [eof $f]
  4427.     lappend l [read $f 3]
  4428.     lappend l [tell $f]
  4429.     lappend l [eof $f]
  4430.     close $f
  4431.     set l
  4432. } "7 [list a\rb] 3 [list \rc\n] 7 0 {} 7 1"
  4433. test io-26.4 {testing crlf reading, leftover cr disgorgment} {
  4434.     removeFile test1
  4435.     set f [open test1 w]
  4436.     fconfigure $f -translation lf
  4437.     puts -nonewline $f "a\rb\rc\r\n"
  4438.     close $f
  4439.     set f [open test1 r]
  4440.     set l ""
  4441.     lappend l [file size test1]
  4442.     fconfigure $f -translation crlf
  4443.     lappend l [read $f 3]
  4444.     lappend l [tell $f]
  4445.     lappend l [gets $f]
  4446.     lappend l [tell $f]
  4447.     lappend l [eof $f]
  4448.     lappend l [gets $f]
  4449.     lappend l [tell $f]
  4450.     lappend l [eof $f]
  4451.     close $f
  4452.     set l
  4453. } "7 [list a\rb] 3 [list \rc] 7 0 {} 7 1"
  4454. test io-26.5 {testing crlf reading, leftover cr disgorgment} {
  4455.     removeFile test1
  4456.     set f [open test1 w]
  4457.     fconfigure $f -translation lf
  4458.     puts -nonewline $f "a\rb\rc\r\n"
  4459.     close $f
  4460.     set f [open test1 r]
  4461.     set l ""
  4462.     lappend l [file size test1]
  4463.     fconfigure $f -translation crlf
  4464.     lappend l [set x [gets $f]]
  4465.     lappend l [tell $f]
  4466.     lappend l [gets $f]
  4467.     lappend l [tell $f]
  4468.     lappend l [eof $f]
  4469.     close $f
  4470.     set l
  4471. } [list 7 a\rb\rc 7 {} 7 1]
  4472.     
  4473. test io-27.1 {testing handler deletion} {
  4474.     removeFile test1
  4475.     set f [open test1 w]
  4476.     close $f
  4477.     set f [open test1 r]
  4478.     testchannelevent $f add readable [list delhandler $f]
  4479.     proc delhandler {f} {
  4480.     global z
  4481.     set z called
  4482.     testchannelevent $f delete 0
  4483.     }
  4484.     set z not_called
  4485.     update
  4486.     close $f
  4487.     set z
  4488. } called
  4489. test io-27.2 {testing handler deletion with multiple handlers} {
  4490.     removeFile test1
  4491.     set f [open test1 w]
  4492.     close $f
  4493.     set f [open test1 r]
  4494.     testchannelevent $f add readable [list delhandler $f 1]
  4495.     testchannelevent $f add readable [list delhandler $f 0]
  4496.     proc delhandler {f i} {
  4497.     global z
  4498.     lappend z "called delhandler $f $i"
  4499.     testchannelevent $f delete 0
  4500.     }
  4501.     set z ""
  4502.     update
  4503.     close $f
  4504.     string compare [string tolower $z] \
  4505.     [list [list called delhandler $f 0] [list called delhandler $f 1]]
  4506. } 0
  4507. test io-27.3 {testing handler deletion with multiple handlers} {
  4508.     removeFile test1
  4509.     set f [open test1 w]
  4510.     close $f
  4511.     set f [open test1 r]
  4512.     testchannelevent $f add readable [list notcalled $f 1]
  4513.     testchannelevent $f add readable [list delhandler $f 0]
  4514.     set z ""
  4515.     proc notcalled {f i} {
  4516.     global z
  4517.     lappend z "notcalled was called!! $f $i"
  4518.     }
  4519.     proc delhandler {f i} {
  4520.     global z
  4521.     testchannelevent $f delete 1
  4522.     lappend z "delhandler $f $i called"
  4523.     testchannelevent $f delete 0
  4524.     lappend z "delhandler $f $i deleted myself"
  4525.     }
  4526.     set z ""
  4527.     update
  4528.     close $f
  4529.     string compare [string tolower $z] \
  4530.     [list [list delhandler $f 0 called] \
  4531.           [list delhandler $f 0 deleted myself]]
  4532. } 0
  4533. test io-27.4 {testing handler deletion vs reentrant calls} {
  4534.     removeFile test1
  4535.     set f [open test1 w]
  4536.     close $f
  4537.     set f [open test1 r]
  4538.     testchannelevent $f add readable [list delrecursive $f]
  4539.     proc delrecursive {f} {
  4540.     global z u
  4541.     if {"$u" == "recursive"} {
  4542.         testchannelevent $f delete 0
  4543.         lappend z "delrecursive deleting recursive"
  4544.     } else {
  4545.         lappend z "delrecursive calling recursive"
  4546.         set u recursive
  4547.         update
  4548.     }
  4549.     }
  4550.     set u toplevel
  4551.     set z ""
  4552.     update
  4553.     close $f
  4554.     string compare [string tolower $z] \
  4555.     {{delrecursive calling recursive} {delrecursive deleting recursive}}
  4556. } 0
  4557. test io-27.5 {testing handler deletion vs reentrant calls} {
  4558.     removeFile test1
  4559.     set f [open test1 w]
  4560.     close $f
  4561.     set f [open test1 r]
  4562.     testchannelevent $f add readable [list notcalled $f]
  4563.     testchannelevent $f add readable [list del $f]
  4564.     proc notcalled {f} {
  4565.     global z
  4566.     lappend z "notcalled was called!! $f"
  4567.     }
  4568.     proc del {f} {
  4569.     global z u
  4570.     if {"$u" == "recursive"} {
  4571.         testchannelevent $f delete 1
  4572.         testchannelevent $f delete 0
  4573.         lappend z "del deleted notcalled"
  4574.         lappend z "del deleted myself"
  4575.     } else {
  4576.         set u recursive
  4577.         lappend z "del calling recursive"
  4578.         update
  4579.         lappend z "del after update"
  4580.     }
  4581.     }
  4582.     set z ""
  4583.     set u toplevel
  4584.     update
  4585.     close $f
  4586.     string compare [string tolower $z] \
  4587.     [list {del calling recursive} {del deleted notcalled} \
  4588.           {del deleted myself} {del after update}]
  4589. } 0
  4590. test io-27.6 {testing handler deletion vs reentrant calls} {
  4591.     removeFile test1
  4592.     set f [open test1 w]
  4593.     close $f
  4594.     set f [open test1 r]
  4595.     testchannelevent $f add readable [list second $f]
  4596.     testchannelevent $f add readable [list first $f]
  4597.     proc first {f} {
  4598.     global u z
  4599.     if {"$u" == "toplevel"} {
  4600.         lappend z "first called"
  4601.         set u first
  4602.         update
  4603.         lappend z "first after update"
  4604.     } else {
  4605.         lappend z "first called not toplevel"
  4606.     }
  4607.     }
  4608.     proc second {f} {
  4609.     global u z
  4610.     if {"$u" == "first"} {
  4611.         lappend z "second called, first time"
  4612.         set u second
  4613.         testchannelevent $f delete 0
  4614.     } elseif {"$u" == "second"} {
  4615.         lappend z "second called, second time"
  4616.         testchannelevent $f delete 0
  4617.     } else {
  4618.         lappend z "second called, cannot happen!"
  4619.         testchannelevent $f removeall
  4620.     }
  4621.     }
  4622.     set z ""
  4623.     set u toplevel
  4624.     update
  4625.     close $f
  4626.     string compare [string tolower $z] \
  4627.     [list {first called} {first called not toplevel} \
  4628.           {second called, first time} {second called, second time} \
  4629.           {first after update}]
  4630. } 0
  4631.  
  4632. test io-28.1 {Test old socket deletion on Macintosh} {socket} {
  4633.     set x 0
  4634.     set result ""
  4635.     proc accept {s a p} {
  4636.     global x wait
  4637.     fconfigure $s -blocking off
  4638.     puts $s "sock[incr x]"
  4639.     close $s
  4640.     set wait done
  4641.     }
  4642.     set ss [socket -server accept 2831]
  4643.     set wait ""
  4644.     set cs [socket [info hostname] 2831]
  4645.     vwait wait
  4646.     lappend result [gets $cs]
  4647.     close $cs
  4648.  
  4649.     set wait ""
  4650.     set cs [socket [info hostname] 2831]
  4651.     vwait wait
  4652.     lappend result [gets $cs]
  4653.     close $cs
  4654.  
  4655.     set wait ""
  4656.     set cs [socket [info hostname] 2831]
  4657.     vwait wait
  4658.     lappend result [gets $cs]
  4659.     close $cs
  4660.  
  4661.     set wait ""
  4662.     set cs [socket [info hostname] 2831]
  4663.     vwait wait
  4664.     lappend result [gets $cs]
  4665.     close $cs
  4666.     close $ss
  4667.     set result
  4668. } {sock1 sock2 sock3 sock4}
  4669.  
  4670. test io-29.1 {TclCopyChannel} {
  4671.     removeFile test1
  4672.     set f1 [open [info script]]
  4673.     set f2 [open test1 w]
  4674.     fcopy $f1 $f2 -command { # }
  4675.     catch { fcopy $f1 $f2 } msg
  4676.     close $f1
  4677.     close $f2
  4678.     string compare $msg "channel \"$f1\" is busy"
  4679. } {0}
  4680. test io-29.2 {TclCopyChannel} {
  4681.     removeFile test1
  4682.     set f1 [open [info script]]
  4683.     set f2 [open test1 w]
  4684.     set f3 [open [info script]]
  4685.     fcopy $f1 $f2 -command { # }
  4686.     catch { fcopy $f3 $f2 } msg
  4687.     close $f1
  4688.     close $f2
  4689.     close $f3
  4690.     string compare $msg "channel \"$f2\" is busy"
  4691. } {0}
  4692. test io-29.3 {TclCopyChannel} {
  4693.     removeFile test1
  4694.     set f1 [open [info script]]
  4695.     set f2 [open test1 w]
  4696.     fconfigure $f1 -translation lf -blocking 0
  4697.     fconfigure $f2 -translation cr -blocking 0
  4698.     set s0 [fcopy $f1 $f2]
  4699.     set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
  4700.     close $f1
  4701.     close $f2
  4702.     set s1 [file size [info script]]
  4703.     set s2 [file size test1]
  4704.     if {("$s1" == "$s2") && ($s0 == $s1)} {
  4705.         lappend result ok
  4706.     }
  4707.     set result
  4708. } {0 0 ok}
  4709. test io-29.4 {TclCopyChannel} {
  4710.     removeFile test1
  4711.     set f1 [open [info script]]
  4712.     set f2 [open test1 w]
  4713.     fconfigure $f1 -translation lf -blocking 0
  4714.     fconfigure $f2 -translation cr -blocking 0
  4715.     fcopy $f1 $f2 -size 40
  4716.     set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
  4717.     close $f1
  4718.     close $f2
  4719.     lappend result [file size test1]
  4720. } {0 0 40}
  4721. test io-29.5 {TclCopyChannel} {
  4722.     removeFile test1
  4723.     set f1 [open [info script]]
  4724.     set f2 [open test1 w]
  4725.     fconfigure $f1 -translation lf -blocking 0
  4726.     fconfigure $f2 -translation lf -blocking 0
  4727.     fcopy $f1 $f2 -size -1
  4728.     set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
  4729.     close $f1
  4730.     close $f2
  4731.     set s1 [file size [info script]]
  4732.     set s2 [file size test1]
  4733.     if {"$s1" == "$s2"} {
  4734.         lappend result ok
  4735.     }
  4736.     set result
  4737. } {0 0 ok}
  4738. test io-29.6 {TclCopyChannel} {
  4739.     removeFile test1
  4740.     set f1 [open [info script]]
  4741.     set f2 [open test1 w]
  4742.     fconfigure $f1 -translation lf -blocking 0
  4743.     fconfigure $f2 -translation lf -blocking 0
  4744.     set s0 [fcopy $f1 $f2 -size [expr [file size [info script]] + 5]]
  4745.     set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
  4746.     close $f1
  4747.     close $f2
  4748.     set s1 [file size [info script]]
  4749.     set s2 [file size test1]
  4750.     if {("$s1" == "$s2") && ($s0 == $s1)} {
  4751.         lappend result ok
  4752.     }
  4753.     set result
  4754. } {0 0 ok}
  4755. test io-29.7 {TclCopyChannel} {
  4756.     removeFile test1
  4757.     set f1 [open [info script]]
  4758.     set f2 [open test1 w]
  4759.     fconfigure $f1 -translation lf -blocking 0
  4760.     fconfigure $f2 -translation lf -blocking 0
  4761.     fcopy $f1 $f2
  4762.     set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
  4763.     set s1 [file size [info script]]
  4764.     set s2 [file size test1]
  4765.     close $f1
  4766.     close $f2
  4767.     if {"$s1" == "$s2"} {
  4768.         lappend result ok
  4769.     }
  4770.     set result
  4771. } {0 0 ok}
  4772. test io-29.8 {TclCopyChannel} {stdio} {
  4773.     removeFile test1
  4774.     removeFile pipe
  4775.     set f1 [open pipe w]
  4776.     fconfigure $f1 -translation lf
  4777.     puts $f1 {
  4778.     puts ready
  4779.     gets stdin
  4780.     set f1 [open [info script] r]
  4781.     fconfigure $f1 -translation lf
  4782.     puts [read $f1 100]
  4783.     close $f1
  4784.     }
  4785.     close $f1
  4786.     set f1 [open "|[list $tcltest pipe]" r+]
  4787.     fconfigure $f1 -translation lf
  4788.     gets $f1
  4789.     puts $f1 ready
  4790.     flush $f1
  4791.     set f2 [open test1 w]
  4792.     fconfigure $f2 -translation lf
  4793.     set s0 [fcopy $f1 $f2 -size 40]
  4794.     catch {close $f1}
  4795.     close $f2
  4796.     list $s0 [file size test1]
  4797. } {40 40}
  4798.  
  4799. test io-30.1 {CopyData} {
  4800.     removeFile test1
  4801.     set f1 [open [info script]]
  4802.     set f2 [open test1 w]
  4803.     fconfigure $f1 -translation lf -blocking 0
  4804.     fconfigure $f2 -translation cr -blocking 0
  4805.     fcopy $f1 $f2 -size 0
  4806.     set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
  4807.     close $f1
  4808.     close $f2
  4809.     lappend result [file size test1]
  4810. } {0 0 0}
  4811. test io-30.2 {CopyData} {
  4812.     removeFile test1
  4813.     set f1 [open [info script]]
  4814.     set f2 [open test1 w]
  4815.     fconfigure $f1 -translation lf -blocking 0
  4816.     fconfigure $f2 -translation cr -blocking 0
  4817.     fcopy $f1 $f2 -command {set s0}
  4818.     set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
  4819.     vwait s0
  4820.     close $f1
  4821.     close $f2
  4822.     set s1 [file size [info script]]
  4823.     set s2 [file size test1]
  4824.     if {("$s1" == "$s2") && ($s0 == $s1)} {
  4825.         lappend result ok
  4826.     }
  4827.     set result
  4828. } {0 0 ok}
  4829. test io-30.3 {CopyData: background read underflow} {unixOnly} {
  4830.     removeFile test1
  4831.     removeFile pipe
  4832.     set f1 [open pipe w]
  4833.     puts $f1 {
  4834.     puts ready
  4835.     flush stdout                ;# Don't assume line buffered!
  4836.     fcopy stdin stdout -command { set x }
  4837.     vwait x
  4838.     set f [open test1 w]
  4839.     fconfigure $f -translation lf
  4840.     puts $f "done"
  4841.     close $f
  4842.     }
  4843.     close $f1
  4844.     set f1 [open "|[list $tcltest pipe]" r+]
  4845.     set result [gets $f1]
  4846.     puts $f1 line1
  4847.     flush $f1
  4848.     lappend result [gets $f1]
  4849.     puts $f1 line2
  4850.     flush $f1
  4851.     lappend result [gets $f1]
  4852.     close $f1
  4853.     after 500
  4854.     set f [open test1]
  4855.     lappend result [read $f]
  4856.     close $f
  4857.     set result
  4858. } "ready line1 line2 {done\n}"
  4859. test io-30.4 {CopyData: background write overflow} {unixOnly} {
  4860.     set big aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n
  4861.     for {set x 0} {$x < 12} {incr x} {
  4862.     append big $big
  4863.     }
  4864.     removeFile test1
  4865.     removeFile pipe
  4866.     set f1 [open pipe w]
  4867.     puts $f1 {
  4868.     puts ready
  4869.     fcopy stdin stdout -command { set x }
  4870.     vwait x
  4871.     set f [open test1 w]
  4872.     fconfigure $f -translation lf
  4873.     puts $f "done"
  4874.     close $f
  4875.     }
  4876.     close $f1
  4877.     set f1 [open "|[list $tcltest pipe]" r+]
  4878.     set result [gets $f1]
  4879.     fconfigure $f1 -blocking 0
  4880.     puts $f1 $big
  4881.     flush $f1
  4882.     after 500
  4883.     set result ""
  4884.     fileevent $f1 read {
  4885.     append result [read $f1 1024]
  4886.     if {[string length $result] >= [string length $big]} {
  4887.         set x done
  4888.     }
  4889.     }
  4890.     vwait x
  4891.     close $f1
  4892.     set big {}
  4893.     set x
  4894. } done
  4895.  
  4896. proc FcopyTestAccept {sock args} {
  4897.     after 1000 "close $sock"
  4898. }
  4899. proc FcopyTestDone {bytes {error {}}} {
  4900.     global fcopyTestDone
  4901.     if {[string length $error]} {
  4902.     set fcopyTestDone 1
  4903.     } else {
  4904.     set fcopyTestDone 0
  4905.     }
  4906. }
  4907. if [catch {socket -server FcopyTestAccept 2828} listen] {
  4908.     puts stderr "Skipping fcopy error test"
  4909. } else {
  4910.     test io-30.5 {CopyData: error during fcopy} {
  4911.     set in [open [info script]]    ;# 126 K
  4912.     set out [socket localhost 2828]
  4913.     catch {unset fcopyTestDone}
  4914.     close $listen    ;# This means the socket open never really succeeds
  4915.     fcopy $in $out -command FcopyTestDone
  4916.     if ![info exists fcopyTestDone] {
  4917.         vwait fcopyTestDone        ;# The error occurs here in the b.g.
  4918.     }
  4919.     close $in
  4920.     close $out
  4921.     set fcopyTestDone    ;# 1 for error condition
  4922.     } 1
  4923. }
  4924. test io-30.6 {CopyData: error during fcopy} {stdio} {
  4925.     removeFile pipe
  4926.     removeFile test1
  4927.     catch {unset fcopyTestDone}
  4928.     set f1 [open pipe w]
  4929.     puts $f1 "exit 1"
  4930.     close $f1
  4931.     set in [open "|[list $tcltest pipe]" r+]
  4932.     set out [open test1 w]
  4933.     fcopy $in $out -command [list FcopyTestDone]
  4934.     if ![info exists fcopyTestDone] {
  4935.     vwait fcopyTestDone
  4936.     }
  4937.     catch {close $in}
  4938.     close $out
  4939.     set fcopyTestDone    ;# 0 for plain end of file
  4940. } {0}
  4941.  
  4942. test io-31.1 {Recursive channel events} {socket} {
  4943.     # This test checks to see if file events are delivered during recursive
  4944.     # event loops when there is buffered data on the channel.
  4945.  
  4946.     proc accept {s a p} {
  4947.     global as
  4948.     fconfigure $s -translation lf
  4949.     puts $s "line 1\nline2\nline3"
  4950.     flush $s
  4951.     set as $s
  4952.     }
  4953.     proc readit {s next} {
  4954.     global result x
  4955.     lappend result $next
  4956.     if {$next == 1} {
  4957.         fileevent $s readable [list readit $s 2]
  4958.         vwait x
  4959.     }
  4960.     incr x
  4961.     }
  4962.     set ss [socket -server accept 2828]
  4963.  
  4964.     # We need to delay on some systems until the creation of the
  4965.     # server socket completes.
  4966.  
  4967.     set done 0
  4968.     for {set i 0} {$i < 10} {incr i} {
  4969.     if {![catch {set cs [socket [info hostname] 2828]}]} {
  4970.         set done 1
  4971.         break
  4972.     }
  4973.     after 100
  4974.     }
  4975.     if {$done == 0} {
  4976.     close $ss
  4977.     error "failed to connect to server"
  4978.     }
  4979.     set result {}
  4980.     set x 0
  4981.     vwait as
  4982.     fconfigure $cs -translation lf
  4983.     lappend result [gets $cs]
  4984.     fconfigure $cs -blocking off
  4985.     fileevent $cs readable [list readit $cs 1]
  4986.     set a [after 2000 { set x failure }]
  4987.     vwait x
  4988.     after cancel $a
  4989.     close $as
  4990.     close $ss
  4991.     close $cs
  4992.     list $result $x
  4993. } {{{line 1} 1 2} 2}
  4994. test io-31.2 {Testing for busy-wait in recursive channel events} {socket} {
  4995.     set s [socket -server accept 3939]
  4996.     proc accept {s a p} {
  4997.     global counter
  4998.  
  4999.     set counter 0
  5000.     fconfigure $s -blocking off -buffering line -translation lf
  5001.     fileevent $s readable "doit $s"
  5002.     }
  5003.     proc doit {s} {
  5004.     global counter
  5005.  
  5006.     incr counter
  5007.     set l [gets $s]
  5008.     if {"$l" == ""} {
  5009.         fileevent $s readable "doit1 $s"
  5010.         after 1000 newline
  5011.     }
  5012.     }
  5013.     proc doit1 {s} {
  5014.     global counter
  5015.  
  5016.     incr counter
  5017.     set l [gets $s]
  5018.     close $s
  5019.     }
  5020.     proc producer {} {
  5021.     global writer
  5022.  
  5023.     set writer [socket localhost 3939]
  5024.     fconfigure $writer -buffering line
  5025.     puts -nonewline $writer hello
  5026.     flush $writer
  5027.     }
  5028.     proc newline {} {
  5029.     global writer done
  5030.  
  5031.     puts $writer hello
  5032.     flush $writer
  5033.     set done 1
  5034.     }
  5035.     producer
  5036.     vwait done
  5037.     close $writer
  5038.     close $s
  5039.     set counter
  5040. } 1
  5041. test io-32.1 {ChannelEventScriptInvoker: deletion} {
  5042.     proc eventScript {fd} {
  5043.     close $fd
  5044.     error "planned error"
  5045.     set ::x whoops
  5046.     }
  5047.     proc bgerror {args} {
  5048.     set ::x got_error
  5049.     }
  5050.     set f [open fooBar w]
  5051.     fileevent $f writable [list eventScript $f]
  5052.     set x not_done
  5053.     vwait x
  5054.     set x
  5055. } {got_error}
  5056.  
  5057. removeFile fooBar
  5058. removeFile longfile
  5059. removeFile script
  5060. removeFile output
  5061. removeFile test1
  5062. removeFile pipe
  5063. removeFile my_script
  5064. removeFile foo
  5065. removeFile bar
  5066. removeFile test2
  5067. removeFile test3
  5068.  
  5069. file delete cat
  5070.  
  5071. set x ""
  5072. unset x
  5073.